Search code examples
iosobjective-cgrand-central-dispatchviewwillappearviewdidappear

Grand Central Dispatch, viewWillAppear, viewDidAppear order of execution confusion


I'm using GCD for background downloading in my Tab Bar app.

First step is to do some background downloading in -viewWillAppear: (to setup some basic data before the view is loaded).

Second step is to the rest of the background downloading in -viewDidAppear:

For some reason, the dispatch block in -viewDidAppear: gets called before the dispatch block in -viewWillAppear:.

This only happens once after loading the application for the first time switching to the tab with the GCD background methods. Switching to another tab and then switching back to the tab with the GCD background methods. The third (and all the rest subsequent times) time I'm switching back it's works as expected (-viewWillAppear: firing first and then -viewDidAppear:).

Here are excerpts of my code (-viewWillAppear: and -viewDidAppear:):

-viewWillAppear:

- (void)viewWillAppear:(BOOL)animated {
    DLog(@"viewWillAppear method running");

    [super viewWillAppear:animated];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

        [self setDiskCareerIds:[CareersParser idsFrom:@"disk"]];
        [self setDownloadedCareerIds:[CareersParser idsFrom:@"web"]];


        DLog(@"diskCareerIds after being set in viewWillAppear: %@", [self diskCareerIds])
        DLog(@"downloadedCareerIds after being set in viewWillAppear: %@", [self downloadedCareerIds])

        if ([[self downloadedCareerIds] isEqualToArray:[self diskCareerIds]]) {

            DLog(@"viewWillAppear: ids equal, loading careers from disk.");
            self.careers = [CareersParser loadCareersFromDisk];

            dispatch_async(dispatch_get_main_queue(), ^{

                [self.table reloadData];

                [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];


            });
        }

    });

    //[self downloadData];
}

-viewDidAppear:

- (void)viewDidAppear:(BOOL)animated {
    DLog(@"viewDidAppear method running");

    [super viewDidAppear:animated];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

        if (![[self downloadedCareerIds] isEqualToArray:[self diskCareerIds]]) {

            DLog(@"ids not equal, saving careers to disk.");

            dispatch_async(dispatch_get_main_queue(), ^{

                [self showLoadingView];

            });

            [CareersParser saveCareersToDisk];
            self.careers = [CareersParser loadCareersFromDisk];
        }



        dispatch_async(dispatch_get_main_queue(), ^{

            [self.table reloadData];

            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

            [self removeLoadingView];

        });
    });

    //[self download3];

    //[self downloadData];
}

Check the debug log at Pastie.


Solution

  • Well, you're printing that log message in that first block (the one scheduled in viewWillAppear:) after it has done a bunch of parsing, not when it actually starts executing.

    The thing is that global queue is a concurrent queue. So even though you are scheduling that first block first, it's not surprising that it sometimes falls behind the other block which is executing concurrently with it.

    One simple answer would be to create a serial queue, and then you'll be sure the first block completes before the second one is executed. That seems to be what you want, right?