我正在与Firebase合作管理我的应用程序数据。
到目前为止,Firebase似乎非常适合管理数据,但当我需要从数据库中读取数据时,我遇到了一个问题。。
要从Firebase数据库中读取数据,请执行以下功能
[[[[FIRDatabase database] reference] child:@"Region"] observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
[_regionList addObject:snapshot.value[@"name"]];
[self.collectionView reloadData];
}];
数据收集方式正确,但问题是似乎有点慢。。。
为了让你更好地理解。。。
我必须在
UICollectionView
.. 问题是,在查看
UICollectionView
收集的数据。。。你可以从我在下面发布的视频中看到一个例子。。
VIDEO
你能告诉我哪里错了吗?我似乎已经做了所有正确的事情,但我不理解读取数据的延迟
这是我的密码
- (void)viewDidLoad {
[super viewDidLoad];
_regionList = [[NSMutableArray alloc] init];
_reference = [[DatabaseReference alloc] init];
[self fetchRegion];
}
-(void)fetchRegion {
[[[[FIRDatabase database] reference] child:@"Region"] observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
[_regionList addObject:snapshot.value[@"name"]];
[self.collectionView reloadData];
}];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.regionList.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ChooseRegionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
cell.regionName.text = self.regionList[indexPath.item];
return cell;
}