iOS Firebase如何在Block之外传递快照值/密钥 [英] iOS Firebase How to pass Snapshot values/keys outside of Block
问题描述
我想使用NSStrings和/或NSarrays来成为快照的价值。所以我可以在我的其他代码中使用它们。
这是我的代码
NSString * string1;
NSArray * array1;
Firebase * usersRef = [fb childByAppendingPath:@Parent name];
FQuery * statesRef = [usersRef queryOrderedByChild:@email];
FQuery * specificStateRef = [statesRef queryEqualToValue:@user name];
[specificStateRef observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot * snapshot){
$ b $ // NSLog(@snapshot:%@,snapshot);
NSLog(@key:%@,snapshot.key);
NSLog(@value:%@,snapshot.value);
//帮我在这里
array1 = [[NSarray alloc] initwithObjects:snapshot.value];
string1 = snapshot.key;
}];
所以在这个块之外,如果我写的话。
NSLog(@String1 =%@,string1);
NSLog(@Array1 =%@,array1);
不幸的是,string1和array1的值都等于Nil。如何从Firebase获取值,然后在整个代码中使用它们。我怎样才能从这个区块中获得这些值?感谢你们
现在有了您的问题:
array1 = [[NSarray alloc] initwithObjects:snapshot.value];
你正在初始化你的数组每次运行时都会阻止它。
修复很简单
块外(也许定义为属性,全局var或__block)
@property NSMutableArray * myMutableArray = [NSMutableArray new];
块内
[myMutableArray addObject:snapshot.value];
您可能忽略的一件事是.childAdded事件对于每个子对象都被调用一次,然后对于之后的任何新的子对象。
所以在这种情况下,如果父节点有三个与查询匹配的子节点,块将被调用三次,这是为什么我们在块中添加每个孩子。
如果您想同时读取所有结果,可以使用.value,然后迭代结果。
没有对错,这取决于你的用例。 (.value可能会读取数以千计的结果,这可能会成为内存问题。)
请参阅 Firebase正在检索数据,以了解.childAdded和.value上的详细信息。
另一个说明。处理在块内检索的块之外的数据可能会非常棘手,因为该块需要花费时间来完成。举个例子,比如说,互联网的日子不好,你的速度很慢 - 块之外的代码可能会在块有时间完成之前执行。所以只有当你知道块已经完成时才能处理数据。 (例如,使用块内的数据,设置一个完成的标志或其他机制,在某些情况下.value使这更容易)。
I am really new to Firebase but I am beginning to like it. So I am having a problem with passing the values of snapshots outside the block.
I want to use NSStrings and or NSarrays to become the value of the snapshots. so that I can use them throughout the rest of my code.
Here is my code
NSString *string1;
NSArray *array1;
Firebase *usersRef = [fb childByAppendingPath:@"Parent name"];
FQuery *statesRef = [usersRef queryOrderedByChild:@"email"];
FQuery *specificStateRef = [statesRef queryEqualToValue:@"user name"];
[specificStateRef observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
//NSLog(@"snapshot: %@", snapshot);
NSLog(@" key: %@", snapshot.key);
NSLog(@" value: %@", snapshot.value);
//HELP ME HERE
array1=[[NSarray alloc]initwithObjects:snapshot.value];
string1=snapshot.key;
}];
So outside this block if I wrote.
NSLog(@" String1 = %@", string1);
NSLog(@" Array1 = %@", array1);
Unfortunately, the values of string1 and array1 both equal Nil. How can I get the values from Firebase and then be able to use them throughout the rest of the code. How can I get those values out of the block? Thanks y'all
Now there's your problem:
array1=[[NSarray alloc]initwithObjects:snapshot.value];
You are initializing your array within the block every time it runs.
Fixing is easy
outside the block (perhaps defined as a property, global var or __block)
@property NSMutableArray *myMutableArray = [NSMutableArray new];
inside the block
[myMutableArray addObject: snapshot.value];
One thing you also may be overlooking is that .childAdded events are called once for each child object and then for any new child objects after that.
So in this case if the parent node had three child nodes that matched the query, the block will be called three times, which is why we add each child within the block.
If you want to read in all of the results at the same time, you can use .value and then iterate over the results.
There's no right or wrong, it just depends on your use case. (.value could potentially read in thousands of results which may become a memory issue.)
See Firebase Retrieving Data for specifics on .childAdded and .value.
Oh - one other note. Working with data outside the block that was retrieved inside the block can be tricky as the block will take time to complete. So, say for example, you're having a bad internet day and your speed is slow - the code outside the block may execute before the block has time to finish. So only address the data when you know the block has completed. (e.g. work with the data inside the block, set a completed flag or some other mechanic. In some cases .value makes this easier).
这篇关于iOS Firebase如何在Block之外传递快照值/密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!