0

I am trying to fetch data from Firebase and after that update the table(reloading data).

-(void) findEvents{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();

    // Add a task to the group
    dispatch_group_async(group, queue, ^{
        [self fetchData];
    });

    // Add another task to the group
    dispatch_group_async(group, queue, ^{
        [self reloadTableData];
    });

    // Add a handler function for when the entire group completes
    // It's possible that this will happen immediately if the other methods have already finished
    dispatch_group_notify(group, queue, ^{
        [self enjoyAfterwards];
    });
}
-(void)fetchData{
    [[ref child:@"calendar_event" ] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {

        for(FIRDataSnapshot* snap in snapshot.children){
            [self.allEvents addObject:[[GAEvent alloc] initWithParams: snap]];

        }
    }];
}
-(void)reloadTableData{
    NSLog(@"reloading table data");
}
-(void) enjoyAfterwards{
    NSLog(@"enjoying");
}

This is my reference. But I am getting the following output:

reloading table data
enjoying
// data is fetched

In Swift, I can use callbacks to achieve this. How do I do this in Objective-C?

Community
  • 1
  • 1
triandicAnt
  • 1,328
  • 2
  • 15
  • 40
  • 1
    I don't understand the question. you can use callbacks in ObjC – Bryan Chen Oct 13 '16 at 23:51
  • I am fetching data from firebase and after that reloading a table that is bound to those data. I have added both methods in the async queue but the reloading method is called prior to fetching data method and the table view data is not updated. – triandicAnt Oct 13 '16 at 23:54
  • so you want `fetchData` -> `reloadTableData` -> `enjoyAfterwards`? just make `fetchData` takes a completion callback. – Bryan Chen Oct 13 '16 at 23:56
  • I can't find the syntax for completion handler in objective c .. can you plz add a link to do the same ! – triandicAnt Oct 14 '16 at 00:01
  • a quick search give me this: http://stackoverflow.com/questions/21436831/how-to-write-an-objective-c-completion-block – Bryan Chen Oct 14 '16 at 00:21
  • thanks! Let me try it.. – triandicAnt Oct 14 '16 at 00:29

0 Answers0