有序阵列与Firebase [英] Ordered Arrays with Firebase

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

问题描述

由于Firebase不保证在从基地读取时的顺序,我们可以从这个异步调用得到无序的输出:

  - (void)loadDataFromFirebase 
{
handles = [[NSMutableArray alloc] init];
Firebase * listRef = [[Firebase alloc] initWithUrl:@https://mobilefeast.firebaseIO.com/foodtrucks];
[listRef observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot * snapshot){
NSLog(@%@,snapshot.name);
[句柄addObject:snapshot.value];
[self.handleTableView reloadData];
}];



$ b $ p
$ b

在示例中,从Firebase中读取代码段,我们可以命令打印异步数据,但是对于iOS中的NSArrays来说,这种效果并不好,在这种情况下,hole-y,稀疏数组不存在。从iOS的Firebase中按顺序填充数组的正确方法是什么?我认为这可以通过添加一个.json文件来完成,这个.json文件添加到一个REST调用的URL的末尾,但是想要使用原生的Firebase iOS API。谢谢。

* 编辑:* 寻找一个简单的字母排序。看起来这应该发生在Rest中,并在REST中使用以下URL:

  https://mobilefeast.firebaseio。 com / foodtrucks / .json 

但是我似乎无法复制与iOS Firebase相同的顺序在这个特定的实例中,Firebase的异步特性并不是什么原因导致事件被触发,看似没有订购。在幕后,我们将所有数据保存为字典,甚至数组(请参阅此处的基本原理: https: //www.firebase.com/docs/managing-lists.html )。由于示例代码正在使用 FEventTypeChildAdded 事件类型,所以回调会按照键(在这种情况下为0,1,2,3,...)按字典顺序触发,12,13,14 ...),然后导致你看到的输出。看看这里: https://www.firebase.com/docs/ordered-data。 html ,了解有关排序在Firebase事件中发挥作用的各种方式的详细信息。



根据您的示例代码以及您希望如何更改和增长数据,你有几个选择:

在这种情况下,你可以使用 FEventTypeValue 我们将以正确的顺序向您传递一个预构建的 NSArray 。这看起来像是:

pre $ [listRef observeEventType:FEventTypeValue withBlock:^(FDataSnapshot * snapshot){
NSLog(@值:%@,snapshot.value);
}];

每当任何一个孩子改变时,但是,如果你想逐步建立表格,这个选项可能不太合适。另外一个选择是转移数据结构来利用的内置订购。所以,如果将来会有更多的数据在这里出现,那么你可以把它转移到与JSON等价的东西上:

  {
alfrescocafe:{name:@alfrescocafe,type:Truck},
barshagyro:{name:@BarshaGyro,type :Truck},
bigronsbistro:{name:@BigRonsBistro,type:Cart}
}

最后,最后一个选项将允许您保持数组结构并按顺序触发事件。您需要为每个孩子添加优先级;在这种情况下,优先级将是数组元素的名称。


As Firebase doesn't guarantee order when reading from a base, we can get unordered output from this async call:

-(void)loadDataFromFirebase
{
    handles = [[NSMutableArray alloc] init];
    Firebase* listRef = [[Firebase alloc] initWithUrl:@"https://mobilefeast.firebaseIO.com/foodtrucks"];
    [listRef observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
        NSLog(@"%@", snapshot.name);
        [handles addObject:snapshot.value];
        [self.handleTableView reloadData];
    }];
}

In the example ordered reading snippet from Firebase, we can order the printing of async data, but this doesn't work so well for NSArrays in iOS, where hole-y, sparse arrays don't exist. What is the right way of populating an array, in order, from Firebase in iOS? I think this can be accomplished with a .json tacked onto the end of a URL for a REST call, but want to try to stick with the native Firebase iOS API. Thanks.

*Edit: * looking for a simple alphabetical sort. It looks like this should happen in Rest, and does with the following URL in REST:

https://mobilefeast.firebaseio.com/foodtrucks/.json

But I can't seem to replicate the same ordering with the iOS Firebase API.

解决方案

In this particular instance, the asynchronous nature of Firebase isn't what's causing the events to be triggered seemingly out of order. Behind the scenes we keep all data as dictionaries—even arrays (See here for the rationale: https://www.firebase.com/docs/managing-lists.html). Since the example code is using the FEventTypeChildAdded event type, callbacks are being triggered lexicographically based on the the keys (in this case, 0, 1, 2, 3, ..., 12, 13, 14...) which is then resulting in the output you're seeing. Take a look here: https://www.firebase.com/docs/ordered-data.html for more details on the various ways ordering plays a role in Firebase events.

Based on your example code and how you expect this data to change and grow, you have a few options:

You could just grab the entire thing all at once using FEventTypeValue in which case we'll pass you a prebuilt NSArray in the correct order. This would look like

[listRef observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
  NSLog(@"value: %@", snapshot.value);
}];

This would then trigger every time any of the children changed. However, if you're wanting to build up the table incrementally this option might not be well suited.

Another option could be to shift the structure of the data to take advantage of the built-in ordering. So, if there's going to be more data that goes along here in the future, you could shift it so the JSON equivalent looks like:

{
  "alfrescocafe": { "name": "@alfrescocafe", "type": "Truck" },
  "barshagyro": { "name": "@BarshaGyro", "type": "Truck" },
  "bigronsbistro": { "name": "@BigRonsBistro", "type": "Cart"}
}

Finally, the last option would allow you to keep the array structure and fire the events in order. You would need to add priorities to each child; in this case the priority would be the name of array element.

这篇关于有序阵列与Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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