Re: NSURLRequest and NSOperationQueue
Re: NSURLRequest and NSOperationQueue
- Subject: Re: NSURLRequest and NSOperationQueue
- From: Keith Duncan <email@hidden>
- Date: Sun, 17 Jan 2010 16:20:47 +0000
> Solution 1 would be to have you do a synchronous NSURLConnection in your main method. This will show you everything is working in that thread and that you do indeed get data back from your server.
This isn't a good idea since it limits the cancelabilty of your operation.
> NSRunLoop* currentRunLoop = [NSRunLoop currentRunLoop];
> if ( currentRunLoop )
> {
> // 1. Start the URLConnection!
> mURLConnection = [NSURLConnection connectionWithRequest:mURLRequest delegate:self];
> [mURLConnection start];
>
> // 2. We have a run Loop, so wait until the connection is finished
> while ( !done && ![self isCancelled] )
> {
> // Run the RunLoop!
> NSDate* dateLimit = [[NSDate date] addTimeInterval:0.1];
> [currentRunLoop runUntilDate:dateLimit];
> }
>
> // 3. Report your results to your main thread!
> …
> }
This is polling and is generally a bad idea, also with such a low timeout your thread will thrash. Furthermore it ties the worker thread up until the operation is complete.
You should instead make a 'concurrent' NSOperation subclass as it's described in NSOperation parlance. What it really means is an asynchronous one.
Implement all the required 'concurrent operation' methods, and in -start you do as you were doing, create an NSURLConnection and schedule it in the +[NSRunLoop mainRunLoop]. In the completed callbacks (under error or success conditions) you mark the operation as isFinished.
This makes your operation cancellable, and frees the worker thread up to service other work units.
Keith
_______________________________________________
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