Re: How to perform delayed calls using NSOperationQueue?
Re: How to perform delayed calls using NSOperationQueue?
- Subject: Re: How to perform delayed calls using NSOperationQueue?
- From: Roland King <email@hidden>
- Date: Tue, 22 Jul 2014 09:42:26 +0800
> On 22 Jul 2014, at 8:42 am, Jens Alfke <email@hidden> wrote:
>
> I’m starting to port a pretty complex source base from using NSURLConnection to using NSURLSession. (Primarily because this is the only way to get around the only-four-sockets-per-host limitation.) I thought it was going to be straightforward, until I saw that NSURLSession only supports scheduling delegate calls on an NSOperationQueue, not an NSRunLoop. So that means I now also have to convert a number of runloop dependencies in the same code, which I don’t understand how to do.
>
> Case in point: I have a couple of things that require the use of delayed performs to schedule timing. What’s the equivalent of -performSelector:withObject:afterDelay: (or of NSTimer) for code that’s running under the control of an NSOperationQueue? I know this call won’t work on a queue (because the thread it’s on probably won’t have a runloop), but I’ve looked at the API docs and headers and don’t see anything comparable. Dispatch queues have dispatch_after, and I’d much rather use GCD than NSOperationQueue anyway*, but NSURLSession is forcing my hand.
>
> —Jens
>
> * (I know there’s now an underlyingQueue property on NSOperationQueue, but that’s only in iOS 8 and I can’t rely on that yet.)
You’d think that would be easy but it doesn’t seem that way. To make sure I understand, you have callbacks on your NSOperationQueue from which you want to then delay an operation on that same queue using NSTimer? I would probably dispatch_after() back to the main queue, or a high- or low- priority, a block which then re-enqueued the operation you wanted to perform back on the same operations queue you started with.
ie if you have the operation you want to delay then mail-written code would look something like
NSOperationQueue *myQueue = [ NSOperationQueue currentQueue ]; // if you don’t already know what op queue you’re on
dispatch_after( dispatch_time( DISPATCH_TIME_NOW, delay * NSEC_PER_SEC ) ), dispatch_get_<some>_queue() ){
[ myQueue addOperation:myOperation ];
};
I think that ought to work.
_______________________________________________
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