[英]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 我在数据库中的帖子结构如下
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];
通常,人们将他们要输入的数组追加到
collectionView
或tableView
(例如),但是在您的情况下,您可以[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.