简体   繁体   English

如何从Firebase按时间顺序获取最新帖子?

[英]How to get chronologically latest post from Firebase?

I am working on app in which Facebook like news feed is used,and i am using Firebase as a database. 我正在开发使用像新闻提要这样的Facebook的应用程序,并且我正在使用Firebase作为数据库。 Everything is working fine, I just need to fetch posts, time wise. 一切工作正常,我只需要在时间上获取帖子。

FIRDatabaseQuery * query = [[[_firebaseReference child:@"Demo"]child:@"posts"]queryLimitedToFirst:100];

[query observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
    if (snapshot.exists) {
        postsDictionary = snapshot.value;
        [self createSocialAppDataSource];
    }
}];

The data in postsDictionary is same as in Database,But i want that data (post) to get sorted respect to time,So how to use query? postsDictionary中的数据与Database中的数据相同,但是我希望该数据(post)相对于时间进行排序,那么如何使用查询?

structure of my post in database as follow 我在数据库中的帖子结构如下

在此处输入图片说明 Thanks 谢谢

To filter the nodes on a child property, call queryOrderedByChild . 要过滤子属性上的节点,请调用queryOrderedByChild

Then when you execute the query against the Firebase Database, there will potentially be multiple results. 然后,当您对Firebase数据库执行查询时,可能会有多个结果。 So the snapshot contains a list of those results. 因此,快照包含这些结果的列表。 Even if there is only a single result, the snapshot will contain a list of one result. 即使只有一个结果,快照也将包含一个结果的列表。

So you'll need to loop over the children: 因此,您需要遍历子级:

FIRDatabaseQuery * query = [[[[_firebaseReference child:@"Demo"] child:@"posts"] queryOrderedByChild: "dateTime"] queryLimitedToFirst:100];

[query observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
    for ( FDataSnapshot *child in snapshot.children) {

        NSLog(@"child.key = %@",child.key);

    }
}];

Loosely based on my answer here: IOS Firebase: Loop all key in snapshot.value get messy position 松散地基于我在这里的答案: IOS Firebase:循环快照中的所有键。值变得混乱

Usually people append the array they are feeding into the collectionView or tableView (for example) but in your case you can [myMutableArray insertObject:myObject atIndex:0]; 通常,人们将他们要输入的数组追加到collectionViewtableView (例如),但是在您的情况下,您可以[myMutableArray insertObject:myObject atIndex:0]; now when you enumerate through your snapshot each post will be added to the front of your array 现在,当您枚举snapshot每个帖子都将添加到array的前面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何按时间顺序从Firebase正确订购数据 - How do you properly order data from Firebase chronologically 如何从iOS中的Firebase数据库获取最新消息 - how to get latest message from firebase database in ios Firebase queryOrderedByKey无法按时间排序帖子 - Firebase queryOrderedByKey not sorting posts chronologically 如何从 firebase 获取嵌套数据库中的每个帖子? - How to get every post inside nested database from firebase? 如何更新Firebase-Database树中的最新条目? - How to update latest entry from Firebase-Database tree? 如何从NSFetchedResultsController获取有限制的最新对象? - How to get the latest objects from NSFetchedResultsController with a limit? 如何从 Swift 中的数组中获取最新日期 - How to get the latest date from array in Swift 如何在最新的 Firebase IOS SDK 中获取 Firebase JWT 令牌以在服务器端进行验证 - How to get Firebase JWT token to validate on server side in Latest Firebase IOS SDK 是否有命令从 Firebase App Distribution 获取最新版本号,就像 TestFlight latest_testflight_build_number 一样? - Is there a command to get latest build number from Firebase App Distribution just like TestFlight latest_testflight_build_number? 无法从Podfile更新Firebase最新框架 - Unable to update firebase latest framework from podfile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM