Dave, 

If you are setting up NSURLConnection on an NSOperation, I would suggest you 
keep the operation around as you get the data back.  The symptom you describe, 
sounds like you are starting the NSURLConnection, but then you leave your main 
method in the NSOperation you created.  This essentially orphans the thread 
that the NSURLConnection needs to report back progress to your delegate.  If 
that Thread no longer exists, the NSURLConnection will never report back.

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.

Solution 2 would be to change your NSOperation main method such that you start 
the NSURLConnection asynchronously, and you will need to loop until either you 
receive the connection:didFailWithError: delegate callback or you receive the 
connectionDidFinishLoading: delegate method callback.

Since you're creating a NSOperation anyway, you probably want threaded access 
to your data. So I would work on solution #2.  Your main method in your 
NSOperation subclass should be something simple like the following:

NSAutoreleasePool*      pool = [[NSAutoreleasePool alloc] init];
        
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!
        ...
}


Scott Tury

On Jan 17, 2010, at 12:08 AM, [email protected] wrote:

> Subject: NSURLRequest and NSOperationQueue
> 
> Hi everyone,
> 
> I'm building an object that communicates with a server.  For various reasons, 
> I'd like to queue up all the NSURLRequests in an NSOperationQueue so that I 
> never have more than one connection open at a time.
> 
> However, I'm running into a weird issue.  If I create my NSURLRequest and 
> open an NSURLConnection directly, then the connection works and everything 
> proceeds as expected.  However, if I create an NSInvocationOperation to delay 
> the creation of the connection until the queue is idle, then the connection 
> is created (and is non-nil), but the URLRequest never triggers any of its 
> delegate methods.
> 
> After some investigation, I realized that the operation was executing on a 
> different thread, so I scheduled the URLConnection on the mainRunLoop in the 
> default mode (after unscheduling from the currentRunLoop).  I'm also 
> retaining the URLConnection in an ivar, but it's still not firing any 
> delegate methods (on any thread).
> 
> Any ideas why my URL connection isn't working?
> 
> Thanks,
> 
> Dave DeLong



_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to