Search code examples
iphoneiosstoryboardobjective-c-blocksuistoryboardsegue

iOS - prepareForSegue doesn't wait for the completionBlock to be completed


What I like: Wait until data download completes then open TableView and show data.

What I have: When prepareForSegue is called the TableView opens immediately without waiting for the data download although I have a completionBlock (which may not be implemented correctly I guess).

Note: When I go back and open the TableView again I see the data.

- (void)fetchEntries
{
    void (^completionBlock) (NSArray *array, NSError *err) = ^(NSArray *array, NSError *err)
    {
        if (!err)
        {
            self.articlesArray = [NSArray array];
            self.articlesArray = array;
        }
    };

    [[Store sharedStore] fetchArticlesWithCompletion:completionBlock];
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [self fetchEntries];

    if ([[segue identifier] isEqualToString:@"ShowArticles"])
    {
        TableVC *tbc = segue.destinationViewController;
        tbc.articlesArrayInTableVC = self.articlesArray;
    }

}

Store.m

- (void)fetchArticlesWithCompletion:(void (^) (NSArray *channelObjectFromStore, NSError *errFromStore))blockFromStore
{
    NSString *requestString = [API getLatestArticles];

    NSURL *url = [NSURL URLWithString:requestString];

    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    Connection *connection = [[Connection alloc] initWithRequest:req];

    [connection setCompletionBlockInConnection:blockFromStore];

    [connection start];
}

Solution

  • You should load your data before you perform a seque.

    - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // show loading indicator
        __weak typeof(self) weakSelf = self;
        [[Store sharedStore] fetchArticlesWithCompletion:^(NSArray *array, NSError *err)
        {
            [weakSelf performSegueWithIdentifier:@"ShowArticles" sender:weakSelf];
            // hide loading indicator
        }];
    }
    
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        // do whatever
    }
    

    Although in my opinion it is much nicer to immediately show the next view controller in response to a user interaction. Have you considered loading your data in the next view controller, instead of waiting for it before you actually want to transition?