0

I've used NSURLConnection a thousand times. This one is not sending the request, or calling the delegate callbacks. If I set the delegate to nil, the request does go out over the wire. No callbacks, of course. If I set the delegate to some other random object in my project that happens to conform to NSURLConnectionDelegate, then the callbacks work.

Why would NSURLConnection fail for a specific delegate instance?

This has been confirmed to be on the main thread, the main run loop and the problematic object answers YES to conformsToProtocol: for the appropriate protocols. It's a singleton and the breakpoint in dealloc is never hit.

Adding code here. It's nothing out of the ordinary nor does it add anything beyond what I said. self is the correct object that fails; Toolbar... is the random one that gets callbacks.

    mSessionCookieConnection = [ NSURLConnection connectionWithRequest: cookieReq
                                                              delegate: self ];///[ ToolBarHeaderViewController sharedToolBarHeaderViewController ] ];
NSLog( @"Conforms to protocols: %d %d", [ self.class conformsToProtocol: @protocol( NSURLConnectionDataDelegate ) ],
                                         [ self.class conformsToProtocol: @protocol( NSURLConnectionDelegate ) ] );
NSLog( @"Is main thread %@", ([NSThread isMainThread] ? @"Yes" : @" NOT" ));
NSLog( @"%@ the main run loop.", [[ NSRunLoop currentRunLoop ] isEqual: [ NSRunLoop mainRunLoop ]] ? @"Using" : @"Not using" );

The output is:

2015-05-07 08:10:50.201 ZZZ[5889:11316419] Conforms to protocols: 1 1
2015-05-07 08:10:50.201 ZZZ[5889:11316419] Is main thread Yes
2015-05-07 08:10:50.202 ZZZ[5889:11316419] Using the main run loop.
Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
BruceL
  • 71
  • 3
  • Out of curiosity - after you set the delegate, if you NSLog your urlConnection.delegate, do you get something non-nil? – charmingToad May 06 '15 at 21:44
  • 2
    Post a little bit of your code please... – FormigaNinja May 07 '15 at 02:52
  • 1
    Share your code please to have a look. – Nicolas Buquet May 07 '15 at 06:45
  • @charmingToad, delegate is not a property. – BruceL May 07 '15 at 12:24
  • It's hard to tell from the code provided if this is pertinent, but keep in mind: NSURLConnection actually keeps a STRONG reference to its delegate, then releases the delegate after its process is complete. Could it be possible that with the other delegates, you are actually relying on those objects to maintain a reference to the NSURLConnection instance, and with the one that fails, both the delegate and the NSURLConnection are being deallocated? – Dave Paul May 07 '15 at 13:24
  • As I said, the dealloc breakpoint is never hit. I have further confirmed the singleton is the only one around by logging its address frequently. In addition, the NSURLConnection is retained because it is an instance variable and it is scheduled in the run loop, which will keep it around until it is done. – BruceL May 07 '15 at 14:22
  • My bad, imagined a property that did not exist. If you log self, is it an instance (such as "") or the class itself (such as "ViewController")? – charmingToad May 07 '15 at 18:51

0 Answers0