iOS - Process data without freezing the UI

Rafael picture Rafael · Aug 10, 2012 · Viewed 11.3k times · Source

I need to do the following tasks:

1) Read some data from sqlite database

2) Process the data

3) With the processed data generate some charts

If I have a user who enters many data in the application, one day this analisys can become slow and freeze the UI.

So, what is the correct way to process it allowing the user to interact with the UI, with the option to cancel the operation or exit the screen?

I need to make simple threads for all my tasks and with a cancel event or flag to stop each one? Or there is another way do to it?

For example:

Task 1: Read the data from sqlite in a thread with a flag to stop the process if needed.

Task 2: Process the data in a thread with a flag to stop the process if needed.

Task 3: Deliver the data to a 3rd party component. At this point, its possible to cancel the operation that is running on other component?

Am I thinking the right way or I could improve on something?

Answer

Sverrisson picture Sverrisson · Aug 10, 2012

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.