Firebase在swift中查询排序顺序? [英] Firebase query sort order in swift?

查看:177
本文介绍了Firebase在swift中查询排序顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将以下Firebase数据库数据加载到我的tableView中时,数据按日期升序排序。

在Xcode中查询:



<$>

如何通过降序来排序(显示最上面的帖子)? ($$$)$ {$ c $} $ {$} $ {$} b

JSON导出:

 posts:{
-KMFYKt7rmfZINetx1hF:{
date:07/09/16 12:46 PM,
postedBy:sJUCytVIWmX7CgmrypqNai8vGBg2,
status:test
},
-KMFYZeJmgvmnqZ4OhT_:{
date:07/09/16 12:47 PM,
postedBy:sJUCytVIWmX7CgmrypqNai8vGBg2,
status:test
},

感谢!!



编辑:下面的代码是整个解决方案,感谢Bawpotter



更新后的查询:

  let ref = self.rootRef.child(上岗 )。queryOrderedByChild( DATE)。observeEventType(.CH ildAdded,withBlock:{(snapshot) - > void 

let post = Post.init(key:snapshot.key,date:snapshot.value![date] as!String,postedBy:snapshot.value![postedBy] as !String,status:snapshot.value![status] as!String)

self.posts.append(post)

self.tableView.insertRowsAtIndexPaths([NSIndexPath (forRow:self.posts.count-1,inSection:0)],withRowAnimation:.Automatic)

tableView cellForRowAtIndexPath

 覆盖func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(PostCell,forIndexPath:indexPath)as! PostCell

self.posts.sortInPlace({$ 0.date> $ 1.date})
self.tableView.reloadData()
pre>

Post.swift:

  import UIKit 

class发布{
var key:String
var date:String
var postedBy:String
var status:String

init(key:String,date:String,postedBy:String,status:String){
self.key = key
self.date = date
self.postedBy = postedBy
self.status = status



$ div class =h2_lin>解决当Firebase将数据加载到您的tableView数据源数组中时,请调用以下代码:

yourDataArray.sortInPlace({$ 0 .date> $ 1.date})



Swift 3版本:

  yourDataArray.sort({$ 0.date> $ 1.date})


When I load the following Firebase Database data into my tableView, the data is sorted in ascending order by date. How can I order this by descending (show the newest post at the top)?

Query in Xcode:

let ref = self.rootRef.child("posts").queryOrderedByChild("date").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in

JSON export:

"posts" : {
    "-KMFYKt7rmfZINetx1hF" : {
      "date" : "07/09/16 12:46 PM",
      "postedBy" : "sJUCytVIWmX7CgmrypqNai8vGBg2",
      "status" : "test"
    },
    "-KMFYZeJmgvmnqZ4OhT_" : {
      "date" : "07/09/16 12:47 PM",
      "postedBy" : "sJUCytVIWmX7CgmrypqNai8vGBg2",
      "status" : "test"
    },

Thanks!!

EDIT: Below code is the entire solution thanks to Bawpotter

Updated query:

let ref = self.rootRef.child("posts").queryOrderedByChild("date").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in

    let post = Post.init(key: snapshot.key, date: snapshot.value!["date"] as! String, postedBy: snapshot.value!["postedBy"] as! String, status: snapshot.value!["status"] as! String)

    self.posts.append(post)

    self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: self.posts.count-1, inSection: 0)], withRowAnimation: .Automatic)

tableView cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("PostCell", forIndexPath: indexPath) as! PostCell

        self.posts.sortInPlace({$0.date > $1.date})
        self.tableView.reloadData()

Post.swift:

import UIKit

class Post {
    var key: String
    var date: String
    var postedBy: String
    var status: String

    init(key: String, date: String, postedBy: String, status: String){
        self.key = key
        self.date = date
        self.postedBy = postedBy
        self.status = status
    }
}

解决方案

When Firebase loads the data into your tableView data source array, call this:

yourDataArray.sortInPlace({$0.date > $1.date})

Swift 3 Version:

yourDataArray.sort({$0.date > $1.date})

这篇关于Firebase在swift中查询排序顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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