Threading for operations that need to run in the background (see bottom of this section for the answer!)
Here's a typically excellent Ray W tutorials (some of following shots & text courtesy of Ray W, hope you don't mind)And an even simpler (yay!) tutorial turbo-charging-your-apps-with-nsoperation/
Notes:
Consists of Task, Thread & Process per this sketch;
NSOperation and NSOperationQueue add a little extra overhead compared to GCD, but you can add dependency among various operations. You can re-use operations, cancel or suspend them. NSOperation is compatible with Key-Value Observation (KVO).
Programatic VC creation w/o storyboard (interesting?)
Here, we wrap our ListViewController in a UINavigationController, and set it as the
root view controller. */ ListViewController *listViewController = [[ListViewController alloc]
initWithStyle:UITableViewStylePlain]; UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:listViewController]; self.window.rootViewController = navController;
As a general rule, if you are executing on a secondary thread and
you must do something to a UIKit object, use performSelectorOnMainThread.
Also, if you want to do something that is related to the UI in the block, you must
do it on the main thread:
Here is the process for a NSOperationQueue
- Instantiate a new NSOperationQueue object
- Create an instance of your NSOperation
- Add your operation to the queue
- Release your operation
Thanks - http://pinkstone.co.uk/how-to-execute-a-method-on-a-background-thread-in-ios/
This uses GCD Grand Central Dispatch, and works well. Updating the UI is on the main thread- (void)loadInBackground
{
NSLog(@"This is for a background thread");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self addPics];
//update UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
//Add the screenMarker array to the layer
[theViewC addScreenMarkers:theMarkers desc:nil];
});
});
}
Now for a Little more complexity
let's try and use NSOperationQueue