【问题标题】:Data from Firebase is unsortedFirebase 中的数据未排序
【发布时间】:2016-11-03 20:02:19
【问题描述】:

我正在尝试按日期对数据进行排序,但数据似乎是随机出现的。谁能告诉我为什么我的queryOrdered(byChild: "startDate") 不起作用?以及我如何对数据进行排序?

你可以在这里看到我的数据结构: Complex Query

代码:

DataService.ds.REF_USER_CURRENT.child("logs").observeSingleEvent(of: .value, with: {(userSnap) in

        if let SnapDict = userSnap.value as? [String:AnyObject]{
            //Run through all the logs
            for each in SnapDict{
                FIRDatabase.database().reference().child("logs/\(each.key)").queryOrdered(byChild: "startDate").observeSingleEvent(of: .value , with : {(Snap) in

                    let period = Period(logId: each.key, postData: Snap.value as! Dictionary<String, AnyObject>)
                    self.periods.append(period)

                    print(period.duration)
                    print(period.startDate)
                    self.tableView.reloadData()
                })
            }
        }
    })

期间:

struct Period {

    var _periodId: String!
    var _startDate: String!
    var _duration: Int!
    var logs = [Log]()

    var _periodRef: FIRDatabaseReference!

    init() {

    }

    init(logId: String, postData: Dictionary<String, AnyObject>) {
        self._periodId = logId

        if let startDate = postData["startDate"] {
            self._startDate = startDate as? String
        }

        if let duration = postData["duration"] {
            self._duration = duration as? Int
        }

        _periodRef = DataService.ds.REF_PERIODS.child(_periodId)
    }
}

【问题讨论】:

  • 你在使用结构吗,如果是的话,用你的结构更新你的问题..
  • 完成@Dravidian :)
  • 当您将快照转换为字典时,它的顺序变得不确定。见stackoverflow.com/questions/40166483/…

标签: ios swift xcode firebase firebase-realtime-database


【解决方案1】:

更改您的数据结构。

保留您的日志索引,例如:-

logs
 -KVMbJKN1i2vAxzutiYq
   duration: 14
   index : 1 
   startDate: 28/10/2016

 -KVMbLL4i_9dwaRZ9REB
   duration: 28
   index : 2
   startDate: 01/09/2016

 -KVMiLwoSY34TZpf8mST
   duration: 14
   index : 3
   startDate: 2/2/2016
totalNoOfLogs : 3

如果您在将数据重新加载到 tableView 之前使用结构,只需对

self.periods.sort(by: {$0.index_Initial > $1.index_Initial})

你的结构:-

struct Period {

var _periodId: String!
var _startDate: String!
var _duration: Int!
var index_Initial : Int!

 ....}

index_Initial 是您的日志索引。检索并存储到您的结构中。

对于 index,您可以在数据库中保留一个单独的节点来跟踪 totalNoOfLogs,并在每次用户创建日志时增加它。

或者你可以试试:-

FIRDatabase.database().reference().child("periods").observeSingleEvent(of: .value, with: {(userSnap) in

        for each in userSnap.children{

            if let eachSnapDict = (each as! FIRDataSnapshot).value as? [String:AnyObject]{


                print(eachSnapDict)

            }
        }
    })

但这种方法只有在您将数据以排序方式存储在数据库中时才有效(不是首选选项)

【讨论】:

  • 感谢您的回答。但是你能告诉我 totalNoOfLogs 是如何工作的吗,因为日志是为所有用户准备的。如果我要将特定用户的日志保存到数据库中,我应该如何计算它? @Dravidian
  • @Dravidan - 我可以看到它现在已排序,仅按我分配给它的索引。但是如何按日期排序呢?
  • 将您保存的日期格式更改为 timestamp,然后使用它代替索引
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 2018-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
相关资源
最近更新 更多