Created
November 23, 2013 21:57
-
-
Save artemstepanenko/7620471 to your computer and use it in GitHub Desktop.
This NSOperationQueue's category solves very simple task. Now you can add completion callback to NSOperationQueue instance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSOperationQueue+Completion.h | |
// QueueTest | |
// | |
// Created by Artem Stepanenko on 23.11.13. | |
// Copyright (c) 2013 Artem Stepanenko. All rights reserved. | |
// | |
typedef void (^NSOperationQueueCompletion) (void); | |
@interface NSOperationQueue (Completion) | |
/** | |
* Remarks: | |
* | |
* 1. Invokes completion handler just a single time when previously added operations are finished. | |
* 2. Completion handler is called in a main thread. | |
*/ | |
- (void)setCompletion:(NSOperationQueueCompletion)completion; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSOperationQueue+Completion.m | |
// QueueTest | |
// | |
// Created by Artem Stepanenko on 23.11.13. | |
// Copyright (c) 2013 Artem Stepanenko. All rights reserved. | |
// | |
#import "NSOperationQueue+Completion.h" | |
@implementation NSOperationQueue (Completion) | |
- (void)setCompletion:(NSOperationQueueCompletion)completion | |
{ | |
NSOperationQueueCompletion copiedCompletion = [completion copy]; | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
[self waitUntilAllOperationsAreFinished]; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
copiedCompletion(); | |
}); | |
}); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage