Re: NSOperationQueue
Re: NSOperationQueue
- Subject: Re: NSOperationQueue
- From: Charles Srstka <email@hidden>
- Date: Sat, 02 Jun 2012 09:57:17 -0500
On Jun 1, 2012, at 10:23 PM, Graham Cox wrote:
> On 02/06/2012, at 1:12 PM, Kyle Sluder wrote:
>
>> Give them a higher priority. You should be able to alter the priorities as the user scrolls, and NSOperationQueue will do the right thing.
>
>
> I tried this but it doesn't work - a bit of thought about how the ops are queued will show why no meaningful priority value can be assigned.
>
> At the moment that the operations are queued, there are some operations in the queue not yet run, and some running. The code that creates the operations doesn't know which ones are needed more urgently (the latest ones), so it can only assign a high priority to all of them, so they all end up with the same (high) priority and so we're back to square one.
Setting the priority seems to work, in my testing:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
const NSUInteger numberOfOperations = 5;
[queue setMaxConcurrentOperationCount:1];
for(NSUInteger i = 0; i < numberOfOperations; i++) {
NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"Running operation number %lu", (unsigned long)i);
sleep(5);
}];
[operation setQueuePriority:NSOperationQueuePriorityNormal];
[queue addOperation:operation];
}
double delayInSeconds = 10.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^{
NSLog(@"Adding prioritized operation");
NSOperation *prioritizedOperation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"Running prioritized operation");
}];
[prioritizedOperation setQueuePriority:NSOperationQueuePriorityVeryHigh];
[queue addOperation:prioritizedOperation];
});
CFRunLoopRun();
}
return 0;
}
produces the following output:
2012-06-02 09:51:14.780 test[2508:1e03] Running operation number 0
2012-06-02 09:51:19.784 test[2508:1e03] Running operation number 1
2012-06-02 09:51:24.779 test[2508:403] Adding prioritized operation
2012-06-02 09:51:24.787 test[2508:1e03] Running prioritized operation
2012-06-02 09:51:24.790 test[2508:1e03] Running operation number 2
2012-06-02 09:51:29.793 test[2508:1e03] Running operation number 3
2012-06-02 09:51:34.797 test[2508:1e03] Running operation number 4
Trying this with some concurrency in place by changing maxConcurrentOperationCount to 5 and numberOfOperations to 25 results in this output:
2012-06-02 09:55:04.995 test[2560:1e03] Running operation number 0
2012-06-02 09:55:04.996 test[2560:1f03] Running operation number 1
2012-06-02 09:55:05.003 test[2560:2003] Running operation number 2
2012-06-02 09:55:05.003 test[2560:2503] Running operation number 3
2012-06-02 09:55:05.007 test[2560:2c03] Running operation number 4
2012-06-02 09:55:10.000 test[2560:2f03] Running operation number 5
2012-06-02 09:55:10.005 test[2560:1e03] Running operation number 6
2012-06-02 09:55:10.006 test[2560:2503] Running operation number 7
2012-06-02 09:55:10.007 test[2560:2003] Running operation number 8
2012-06-02 09:55:10.010 test[2560:2d03] Running operation number 9
2012-06-02 09:55:14.992 test[2560:403] Adding prioritized operation
2012-06-02 09:55:15.005 test[2560:2f03] Running prioritized operation
2012-06-02 09:55:15.007 test[2560:2c07] Running operation number 10
2012-06-02 09:55:15.008 test[2560:1f03] Running operation number 11
2012-06-02 09:55:15.011 test[2560:1e03] Running operation number 12
2012-06-02 09:55:15.012 test[2560:2003] Running operation number 13
2012-06-02 09:55:15.013 test[2560:2d03] Running operation number 14
2012-06-02 09:55:20.010 test[2560:2c07] Running operation number 15
2012-06-02 09:55:20.012 test[2560:1f03] Running operation number 16
2012-06-02 09:55:20.014 test[2560:2507] Running operation number 17
2012-06-02 09:55:20.014 test[2560:2003] Running operation number 18
2012-06-02 09:55:20.016 test[2560:1e03] Running operation number 19
2012-06-02 09:55:25.014 test[2560:2c07] Running operation number 20
2012-06-02 09:55:25.015 test[2560:2507] Running operation number 21
2012-06-02 09:55:25.015 test[2560:1f03] Running operation number 22
2012-06-02 09:55:25.018 test[2560:1e03] Running operation number 23
2012-06-02 09:55:25.021 test[2560:2d03] Running operation number 24
So, it would seem that setting the queue priority does seem to be capable of effectively bumping operations to the front of the queue like you want.
Charles
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden