Reputation: 505
I’ve created simple UITableView with couple of rows that is loading from Firebase. On viewWillAppear I connect observer and populate the array with data. The problem happens when view is already loaded but asynchronous Firebase callback is still wasn’t called - Table appears as blank. The table refreshes after callback and looks like expected but the delay is still noticeable. I’ve configured offline mode and expecting that observer’s handler should be called immidiately but it doesn’t.
What is the proper way to handle that? Waiting bar looks like not the best option because it’s just 100ms or so and data is already on device and I have just 8 rows. Is there any solution? Thank you!
Observe code:
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
refHandle = [areasRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
[self.arrayOfAreas removeAllObjects];
for (FIRDataSnapshot* areaSnapshot in snapshot.children){
Area *area = [[Area alloc] init];
[area retrieveFromSnapshot:areaSnapshot];
[self.arrayOfAreas addObject:area];
} [self.tableView reloadData];
}]; }
viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
self.ref = [[FIRDatabase database] reference];
FIRUser *currentUser = [FIRAuth auth].currentUser;
areasRef = [[[_ref child:@"users"] child:currentUser.uid] child:@"areas"];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.tableFooterView = [[UIView alloc]init];
self.arrayOfAreas = [NSMutableArray new];
self.tableView.allowsMultipleSelectionDuringEditing = NO; }
Upvotes: 1
Views: 534
Reputation: 1192
After fresh install of your app, Obviously there will be a delay to fetch the datas from firebase. once you fetch all the data, you have to save it in your local DB.
From second time onwards, show the datas from your local DB and concurrently fetch the firebase DB also. (if you doesn't do update/delete in inner key values of the fetching path, firebase DB fetching query can be optimised based on the local data).
If you are saving the local DB datas to arrayOfAreas. you can change the observe block as follows
NSMutableArray *arrayTemp = [[NSMutableArray alloc]init];
for (FIRDataSnapshot* areaSnapshot in snapshot.children){
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Area" inManagedObjectContext:[CoreData sharedData].context];
Area *area = [[Area alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
[area retrieveFromSnapshot:areaSnapshot];
[arrayTemp addObject:area];
}
/**
if you are fetching all datas again
*/
self.arrayOfAreas = arrayTemp;
[self.tableView reloadData];
/**
if you are fetching only the datas that are newly added (datas that are not existing in local DB)
*/
[self.arrayOfAreas addObjectsFromArray: arrayTemp];
[self.tableView reloadData];
if you are using firebase offline feature, try below way. (it may not be a perfect solution)
invoke [FIRDatabase database].goOffline
in viewDidLoad or app launch.
once the observer block executed, [FIRDatabase database].goOnline
.
Upvotes: 1
Reputation: 3008
Your UITableview
is getting loaded on viewDidLoad
, as there would be no data in you array at initially your tableView
is blank.
If you don't wish to load your tableView
until the you retrieve data then you may assign your datasource
and delegate
after you finish fetching your records. (In your completion block of Firebase Method)
You can show loader till your data is retrieved.
Upvotes: 0