Question

I tried my app on my iPhone with iOS 7 and everything works perfectly except one thing. On iOS 6 versions, when I executed the following code, the loading view (with the activityindicator) appeared and disappeared at the end of the loading. On iOS 7, the view does not appear at all during the loading.

self.loadingView.alpha = 1.0;
[self performSelector:@selector(accessServices) withObject:nil afterDelay:0.0f];

AccessServices method :

- (void)accessServices {
     // Getting JSON stuff
     // The code is OK, I just don't copy/paste it here

     self.loadingView.alpha = 0.0;
}

What happens ? Is it an iOS 7 bug ?

Was it helpful?

Solution

I wouldn't have expected the behavior of the above code to change, though I'm not surprised that it might not work the way you expect. If you do the JSON stuff on the main queue, your code will be dependent on idiosyncrasies of when the UI update takes place, rather than making it explicit.

You probably want to explicitly dispatch the JSON code to a background queue, and then dispatch the final UI update back to the main queue. The standard pattern for something like this is:

self.loadingView.alpha = 1.0;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Getting JSON stuff
    // The code is OK, I just don't copy/paste it here

    dispatch_async(dispatch_get_main_queue(), ^{
        self.loadingView.alpha = 0.0;
    });
});

You can use GCD (like above) or operation queues, or whatever. But the idea is the same, that UI updates should happen on the main queue but that anything remotely computationally expensive or slow else should happen on a background queue.

This pattern should work regardless of iOS version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top