-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSOperationQueueTimeout.m
62 lines (50 loc) · 1.38 KB
/
NSOperationQueueTimeout.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// NSOperationQueueTimeout.h
// SCZObjCStateMachine
//
// Created by Peter Suk on 6/5/12.
// Copyright (c) 2012 Peter Kwangjun Suk. All rights reserved.
//
#import "NSOperationQueueTimeout.h"
@interface NSOperationQueueTimeout()
@property (nonatomic, strong, readwrite) NSOperationQueue *operationQueue;
@end
@implementation NSOperationQueueTimeout
@synthesize operationQueue;
+ (NSOperationQueueTimeout*)withQueue:(NSOperationQueue*)anOperationQueue
{
return [[NSOperationQueueTimeout alloc] initWithQueue:anOperationQueue];
}
+ (void)withQueue:(NSOperationQueue*)anOperationQueue
interval:(NSTimeInterval)timeInterval
{
[[self withQueue:anOperationQueue] waitUntilAllFinishedOr:timeInterval];
}
-(id)initWithQueue:(NSOperationQueue*)anOperationQueue
{
if (self = [self init])
{
self.operationQueue = anOperationQueue;
}
return self;
}
- (void)cancelAllOperations
{
@synchronized(self)
{
if (self.operationQueue)
{
[self.operationQueue cancelAllOperations];
}
}
}
- (void)waitUntilAllFinishedOr:(NSTimeInterval)timeInterval
{
[self performSelector:@selector(cancelAllOperations)
onThread:[NSThread mainThread]
withObject:self
waitUntilDone:NO];
[self.operationQueue waitUntilAllOperationsAreFinished];
[self cancelAllOperations];
}
@end