【发布时间】:2016-03-16 16:08:42
【问题描述】:
我了解到,为 Firebase 保持数据扁平化以及仅嵌套您打算调用的数据非常重要。我已经完成了这些事情,但 Firebase 在检索数据方面仍然太慢。这是一个例子:
我的数据如下所示:
--English
----Ari : 4
----Philip : 2
----John : 6
我的代码如下所示:
[super viewDidLoad];
[[DataSource sharedInstance].selectedLanguageMutableArray removeAllObjects];
//Retrieving Data From Firebase
NSString* selectedLanguagePath = [NSString stringWithFormat:@"languages/%@", [DataSource sharedInstance].languageSelected];
Firebase *languagesRef = [[DataSource sharedInstance].ref childByAppendingPath:selectedLanguagePath];
[[languagesRef queryOrderedByValue] observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
[self.distanceMutableArray addObject:snapshot.key];
NSLog(@"%@", snapshot.key);
NSLog(@"%@", snapshot.value);
NSLog(@"%@", self.distanceMutableArray);
}];
//Selected Languages Mutable Array
[[DataSource sharedInstance].selectedLanguageMutableArray removeAllObjects];
for (NSInteger i = 0; i < self.distanceMutableArray.count; i++) {
UserCustomizationData *item = [[UserCustomizationData alloc] init];
NSString* selectedUser = self.distanceMutableArray[i];
Firebase* selectedUserRef = [[DataSource sharedInstance].usersRef childByAppendingPath:selectedUser];
if (selectedUser.length > 0) {
Firebase* profilePicRef = [selectedUserRef childByAppendingPath:@"profilePicture"];
[profilePicRef observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
NSString* profPicString = snapshot.value;
NSData *dataFromBase64=[NSData base64DataFromString:profPicString];
UIImage *profPicImage = [[UIImage alloc]initWithData:dataFromBase64];
item.profilePicture = profPicImage;
}];
[[DataSource sharedInstance].selectedLanguageMutableArray addObject:item];
}
}
但是,for 循环在 self.distanceMutableArray 可以填充之前运行。这会抛出所有问题,因为 for 循环依赖于填充的 self.distanceMutableArray。
有没有一种方法可以检索数据,使代码能够流畅地按照编写顺序运行?
【问题讨论】:
-
请编辑您的问题以将 JSON 作为文本包含在内,而不是链接到您的数据结构的图像。链接腐烂。文本图像是 Twitter 的东西,而不是 StackOverflow。
-
循环体有什么作用?可以出示一下代码吗?
-
我做了你问的关于@FrankvanPuffelen的编辑
-
我添加了循环代码@DavidEast
-
最终目标是什么?您是否尝试填充
UITableView?您的设计与异步数据流不匹配。如果您提示我您正在尝试做什么,我可以帮助您制定解决方案以更好地解决您的用例。
标签: ios objective-c firebase data-retrieval