简体   繁体   中英

dispatch_async(dispatch_get_main_queue(), ^{}); from Background Thread slow on iOS7

I found a strange behavior on iOS7. I Have a dispatch_async background thread which handles heavy load and from this thread i want to update UI. So i did just as follows:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

    [self heavyFunction];

    dispatch_async(dispatch_get_main_queue(), ^{

        [self updateUI];

    });
});

On iOS 8 and above the above code works as suggested but running on an iOS7 Test Device (iPhone 4s) the call to dispatch_async(dispatch_get_main_queue(), ^{}); takes 4 seconds everytime. I allready took the update and the heavy_function out and the same behavior still appears. Then i builded up a minimal example, in which it does not appear. So i thought maybe something else is blocking my main_queue on iOS7 but i can't figure out what this should be. There is no other UI related stuff happening on this ViewController until the [self heavyfunction]; finishes.

Everything is compiled with XCode 7.3.1 with Base SDK 9.3 and Deployment Target 7.0.

Does anyone had this behavior already and could point me in a direction what could be blocking here?

So, after some research i found the problem. I call the background thread with a notification (over the NSNotificationCenter ) from my backend and this notification get's catched from two different ViewControllers to update data sources for UITableViews. The second one (the one with the hang) is in NavigationController hierarchy under the first one.

Both notifications get handled the same time and the UI reloads on both controllers, even if the first one isn't visible at all. But this just seem to happen on iOS 7. All above iOS versions just don't reload the UI from the invisible ViewController it seems.

When i jump between ViewControllers everything on the UI get's updated either way, because the data source gets updated from the background thread, so i solved the problem by just checking which of the booth ViewControllers is shown right now and only reload the UI on that one.

What still bugs me is that all iOS versions above iOS 7 seem to handle this problem automatically, but iOS 7 doesn't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM