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

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

问题描述

我将 Firebase 与 Alamofire、AlamofireImage 结合使用,将我的 imageURL 数据缓存在内存中并上传 ImageShack.

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

我一直在尝试创建降序查询并进行搜索,但找不到可能的描述.这是我在 Firebase 上的测试ref_post.

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()

-- 用户名

-- imageUrl

-- imageUrl

-- 时间戳(我使用 这个)

-- timestamp (I have created using this)

*Using NSDate().formattedISO8601 这是最好的方法还是你能建议我基本上处理它吗?

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

如何在 Firbase IOS/Swift 中进行降序查询.这是我的 viewDidLoad:

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()

为了保存 NSDate 实例,我个人使用 timeIntervalSinceReferenceDate() 返回一个 NSTimeInterval(这是一个 Double>),然后您可以将其保存在 Firebase 中.读取数据时,可以通过init(timeIntervalSinceReferenceDate:)获取原始的NSDate.

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天全站免登陆