Rafael Rafael - 5 months ago 79
iOS Question

iOS - Process data without freezing the UI

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

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:

Swift 3.0

DispatchQueue.global().async() {
    print("Work Dispatched")
    // Do heavy or time consuming work

    // Create a weak reference to prevent retain cycle and get nil if self is released before run finishes
    DispatchQueue.main.async() {
        [weak self] in
        // Return data and update on the main thread, all UI calls should be on the main thread
        if let weakSelf = self {
            weakSelf.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).
            });
        }
    });

Swift 2.0 (see new version above)

  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
  print("Work Dispatched")
  // Do heavy or time consuming work

  // Create a weak reference to prevent retain cycle and get nil if self is released before run finishes
     dispatch_async(dispatch_get_main_queue()){
         [weak self] in
         // Task 3: Return data and update on the main thread, all UI calls should be on the main thread
            if let weakSelf = self {
               weakSelf.method()
            }
      }
  }

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

if let weakSelf = self {
   weakSelf.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.