Singleton Issues in iOS http client

Posted by Andrew Lauer Barinov on Stack Overflow See other posts from Stack Overflow or by Andrew Lauer Barinov
Published on 2012-07-07T20:50:30Z Indexed on 2012/07/07 21:15 UTC
Read the original article Hit count: 345

I implemented an HTTP client is iOS that is meant to be a singleton. It is a subclass of AFNetworking's excellent AFHTTPClient

My access method is pretty standard:

+ (LBHTTPClient*) sharedHTTPClient
{    
    static dispatch_once_t once = 0;
    static LBHTTPClient *sharedHTTPClient = nil;

    dispatch_once(&once, ^{
        sharedHTTPClient = [[LBHTTPClient alloc] initHTTPClient];
    });

    return sharedHTTPClient;
}

// my init method - (id) initHTTPClient { self = [super initWithBaseURL:[NSURL URLWithString:kBaseURL]];

if (self) 
{
    // Sub class specific initializations
}

return self;

}

Super's init method is very long, and can be found here

However the problem I experience is when the dispatch_once queue is run, the app locks and become unresponsive.

When I step through code, I notice that it locks on dispatch_once and then remains frozen. Is this something to do with the main thread locking down? How come it stays locked like that?

Also, none of the HTTP client methods fire.

Stepping through the code some more, I find this line in dispatch/once.h is where the app locks down:

DISPATCH_INLINE DISPATCH_ALWAYS_INLINE DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
void
_dispatch_once(dispatch_once_t *predicate, dispatch_block_t block)
{
    if (DISPATCH_EXPECT(*predicate, ~0l) != ~0l) {
        dispatch_once(predicate, block); // App locks here
    }
}

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ios5