Reputation: 33
How can we write the code to run in the background in iPhone Application, same as the Service
OR AsyncTask
in Android?
Upvotes: 0
Views: 184
Reputation:
iOS platform since iOS 3.0 fully supports multithreading and background code execution. There are several ways you can achieve this (thread which is not a main UI thread I will call a background thread). I'll try to cover all:
NSThread- you should use when you want to control you backgound thread fully. You can control thread prioriies with setThreadPriority:
, set thread stack size, names and etc. NSThread
is just a Cocoa wrapper around the pthreads which you can also use, but it is an old way and I even won't consider it. For example and guide on how to work with pthreads or NSThread
look here: https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html
- (void)performSelectorInBackground :
one of the simplest ways. It just runs the specified method on a new background thread: simple and solid. For docs look here:https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/nsobject_class/reference/reference.html#//apple_ref/occ/instm/NSObject/performSelectorInBackground:withObject:
[self performSelectorInBackground:@selector(makeNetworkRequestOrWhateverInBackground:)
withObject:someArgument];
It is identical to NSThread
way with [NSThread detachNewThreadSelector:@selector(makeNetworkRequestOrWhateverInBackground:) toTarget:self withObject:nil];
But I want to emphasize that it is now not
a good way of doing background stuff. performSelector
has a lot of downsides: it’s pretty slow (creates a NSThread
object dynamically, memory overhead, because of storing thread stacks, and so on and so forth), passing data to the method, you want to keep in the background is a real pain in the ass (you have to wrap it up in the withObject
parameter, or make global in your object and global data is a bad thing). Apple also recommends to use GCD
instead of threads and therefore performSelectorInBackground
, look e.g. here. So the third option is what you really need.
GCD - grand central dispatch - is a mainstream technology now. A lot of different third party libs use it like AFNetworking
(therefore libraries built on top of AFNetworking
such as RestKit
). I think you will use this method of multithreading very often. If you just want to parallelize some simple task, make some network call asynchronously, fetch data from the Core Data
, etc. you should use GCD
. This technology is beautiful when combines with Objective-C blocks (and it was designed for usage with them) but you are not only restricted with blocks, you can use it also with regular functions with f-variants of GCD functions. A lot of beautiful Objective-C frameworks is built on top GCD. Simple example of usage:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self makeNetworkRequestOrWhateverInBackground];
});
NSOperationQueue is a higher abstract level of simple threads. An NSOperationQueue
object is a queue that handles objects of the NSOperation
class type which represents a single task, including both the data and the code related to the task. It abstracts the thread concept to operation concept. You can make subclassing, create complex operations dependency graphs, make cancellation and so on and so forth. It supports a number of other higher-level semantics and concepts. Also look at Bolts framework - a very interesting implementation and wrapper for all this stuff from Parse and Facebook. NSOperation
concept IMO is an Apple way of dealing with task-based parallelism, which means that you design your algorithms in terms of "tasks" (work to be done) instead of the specifics of threads and cores. For theory and examples of NSOperation
and NSOperationQueue
look for good tutorials here and here.
Upvotes: 5