iOS - Process data without freezing the UI iOS - Process data without freezing the UI multithreading multithreading

iOS - Process data without freezing the UI


This is the recommended and fastest way by Apple with GCD (Grand Central Dispatch). It´s also easier to read and understand, because the logic is linear but not split between methods.

Note that it shows the weakSelf "dance" which is necessary if i.e. the async may outlive the controller it is called for, thus it makes a weak reference to it and then checks if it is alive and retains it to update:

Swift 4 & 3

 DispatchQueue.global().async() {      print("Work Dispatched")      // Do heavy or time consuming work      // Then return the work on the main thread and update the UI      // Create weak reference to self so that the block will not prevent it to be deallocated before the block is called.      DispatchQueue.main.async() {           [weak self] in           // Return data and update on the main thread, all UI calls should be on the main thread            // Create strong reference to the weakSelf inside the block so that it´s not released while the block is running           guard let strongSelf = self else {return}           strongSelf.method()      } }

Objective-C

// To prevent retain cycles call back by weak reference   __weak __typeof(self) weakSelf = self;  // New C99 uses __typeof(..)    // Heavy work dispatched to a separate thread    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        NSLog(@"Work Dispatched");        // Do heavy or time consuming work        // Task 1: Read the data from sqlite        // Task 2: Process the data with a flag to stop the process if needed (only if this takes very long and may be cancelled often).        // Create strong reference to the weakSelf inside the block so that it´s not released while the block is running        __typeof(weakSelf) strongSelf = weakSelf;        if (strongSelf) {            [strongSelf method];            // When finished call back on the main thread:            dispatch_async(dispatch_get_main_queue(), ^{                // Return data and update on the main thread                // Task 3: Deliver the data to a 3rd party component (always do this on the main thread, especially UI).            });        }    });

The way to cancel the process is to include a BOOL value and set it to stop from the main thread if the work being done is no longer needed. But, it may not be worth it because the user will not notice the background work so much unless it´s heavy calculations.To prevent retain cycles use weak variables like:

__weak __typeof(self) weakSelf = self; // Obj-C[weak self] in

and call weakSelf with strong reference inside the block (to prevent crashes if the calling VC has been released). You can use the exact type like UIViewController or the __typeof() function (in C99 you need to use __typeof(..) but previously you could use __typeof(..) directly) to refer to the actual type:

Objective-C

__typeof(weakSelf) strongSelf = weakSelf;if (strongSelf) {   [strongSelf method];}

Swift 4 & 3

if let weakSelf = self {   weakSelf.method()}

or

// If not referring to self in method calls.self?.method()

NOTE: Use GCD or Grand Central Dispatch, it´s most simple, the recommended way by Apple and the code flow is in logical order.


this is how to detach functions from your main thread

[NSThread detachNewThreadSelector:@selector(yourmethode:) toTarget:self withObject:nil];