CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.
TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by Ajay MT.
Parallel allows you to perform selectors on a background thread and call callbacks after the selectors have been performed.
Here's an example:
- (instancetype)init
{
if (self = [super init])
Parallel *parallel = [[Parallel alloc] init];
return self;
}
- (id)doSomethingThatTakesTime
{
return [something thatTakesTime];
}
- (void)someMethod
{
[parallel performSelector:@selector(doSomethingThatTakesTime)
onTarget:self
withCallback:^(id result){
NSLog(@"done: %@", result);
}];
NSLog(@"doing something that takes time...");
}
This program will log "doing something that takes time...done: ".
Unlike -[NSThread detachNewThread...]
and -[NSObject performSelectorInBackground...]
, Parallel doesn't spawn a new thread every time it performs a selector. Here's how it works:
You call [[Parallel alloc] init]
, which starts a background thread and starts an event loop on that thread. The event loop constantly checks an event queue for new events.
You call -[Parallel performSelector...]
which adds an event to the event queue.
The event loop, running on the background thread, processes the new event. The selector associated with the event is performed and the associated callback block is called on the main thread.
After you're done performing various selectors, you call -[Parallel cancel]
, which shuts down the event loop and the background thread.
You can take the whole thing and drop it in your project, or you can install it with CocoaPods.
Initialize the Parallel object and start a beckground thread & event loop.
Add a selector to the event queue. Once all selectors previously added have been performed, this selector (sel
) is performed on target
on the background thread. Once this selector has been performed, the callback block (callback
) is called on the main thread and the next selector (if any) is performed. callback
is called with one argument: the value returned by sel
.
End the event loop and kill the background thread.
Open the xcodeproj and hit Command-U. If you prefer the terminal, you can do this:
$ xcodebuild test -project Parallel.xcodeproj -scheme Parallel -destination 'platform=OS X,arch=x86_64'
MIT License. See ./LICENSE
for details.