3

I'm building an iPhone app in which I retrieve data from a .json file on a web site and parse it into a some UITextField view in the main view. I want to check when the data is retrieved so that I can then remove the UIActivityIndicator from the screen.

This is the snippet of code I want to check on when it's complete, it's in viewWillAppear:

 //if a connection is available... get and load the infor via .json
if (connection){

    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL:
                        kLatestGigsURL];
        [self performSelectorOnMainThread:@selector(fetchedData:)
                               withObject:data waitUntilDone:YES];
    });
}

Is it possible to check if the data has been fetched? If so how do I do this?

Thanks for reading!

James B.
  • 135
  • 1
  • 11

1 Answers1

2

dataWithContentsOfURL: is a synchronous method, so once it returns you can say it is complete.

Also, I would suggest to you tweak your code to use GCD to perform the block on the main thread like this:

if (connection){

    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL:kLatestGigsURL];
        dispatch_async(dispatch_get_main_queue(), ^{
            // Update your indicator
            [self fetchedData:data];
        });
    });

}
lucianomarisi
  • 1,552
  • 11
  • 24