Swift - 如何在Firebase上按降序创建排序查询? [英] Swift - How to create Sort query as Descending on Firebase?

查看:261
本文介绍了Swift - 如何在Firebase上按降序创建排序查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Firebase与Alamofire,AlamofireImage来缓存我的imageURL数据在内存上传ImageShack。

我坚持创建降序查询试图做和搜索,但我找不到可能的描述。这是我在Firebase上的 ref_post 测试。
$ b childByAutoId()

- userUid

- imageUrl



- timestamp(我使用这个



*使用 NSDate()。formattedISO8601 是最好的方法还是可以建议我来处理它? / p>

如何在Firbase IOS / Swift中执行降序查询。这是我的 viewDidLoad
$ b

  let query = DataService.ds.REF_POSTS.queryOrderedByChild(timestamp) 

query.observeEventType(.Value,withBlock:{snapshot in

if snapshots = snapshot.children.allObjects as?[FDataSnapshot] {
self.posts = $] b $ b为快照捕捉{
如果让postDict = snap.value为?Dictionary< String,AnyObject> {

print(postDict)

let post = Post(imageUrl:postDict [imageUrl]!as?String,username:DataService.ds.REF_USERS.authData.uid)

self.posts.append(post)



self.tableView.reloadData()
}
})


解决方案

  self.posts = self.posts.reverse()

保存 NSDate 我个人使用 timeIntervalSinceReferenceDate(),它返回一个 NSTimeInterval (这是一个 / code>),然后您可以保存在Firebase中。当读取数据时,可以用 init(timeIntervalSinceReferenceDate:)获取原始 NSDate


I'm using Firebase with Alamofire, AlamofireImage to cache my imageURL data on memory and upload ImageShack.

I stuck creating descending query tried to do and search but I couldn't find possible description for me. Here's my test ref_post on Firebase.

childByAutoId()

-- userUid

-- imageUrl

-- timestamp (I have created using this)

*Using NSDate().formattedISO8601 Is it best way or Can you advice me to handle it basically?

How Can I do descending query in Firbase IOS/Swift. Here's my viewDidLoad:

let query = DataService.ds.REF_POSTS.queryOrderedByChild("timestamp")

    query.observeEventType(.Value, withBlock: { snapshot in

        if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {
            self.posts = []
            for snap in snapshots {
                if let postDict = snap.value as? Dictionary<String, AnyObject> {

                    print(postDict)

                    let post = Post(imageUrl: postDict["imageUrl"]! as? String, username: DataService.ds.REF_USERS.authData.uid)

                    self.posts.append(post)
                }
            }

            self.tableView.reloadData()
        }
    })

解决方案

self.posts = self.posts.reverse()

To save NSDate instances, I personally use timeIntervalSinceReferenceDate() which returns an NSTimeInterval (which is a Double), which you can then save in Firebase. When reading the data, you can obtain the original NSDate with init(timeIntervalSinceReferenceDate:).

这篇关于Swift - 如何在Firebase上按降序创建排序查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆