Search code examples
iosobjective-cgcdasyncsocket

ios - load new view as modal and keep gcd async code running


I'm using dispatch_async to run a section of code (a specific method) on a parallel queue (a series of connectivity checks, not really important)

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
        [self connectivityChecks];
});

Everything is working fine with this while on the current view controller.

What I would like to do (if possible) is to load a new view controller modally and keep that section of code running, as it would fire some events on the new view controller based on the checks it's running. Using this to load new view.

CustomViewController *customLoader = [[CustomViewController alloc] init];
[customLoader setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:customLoader animated:YES completion:nil];

Is it at all possible to have this working this way? If not, how could I achieve something along these lines?


Solution

  • I transform my comment into a more detailed answer :

    1. In your CustomViewController, add a notification observer, named, well, say : 'com.myapp.connected'
    2. In the same CustomViewController, don't forget to remove the observer when it disappears
    3. Create a new custom class, say : 'TaskManager'
    4. Insert a method in charge to launch an async task in it

      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
      [self connectivityChecks];
      

      });

    5. In connectivityCheck, post a notification named 'com.myapp.connected' when the connection operation succeeded
    6. From your calling class, create an instance of 'TaskManager'
    7. From your calling class, Create an instance of CustomViewController.