nxtbgthng / OAuth2Client

Client library for OAuth2 (currently built against draft 10 of the OAuth2 spec)
855 stars 217 forks source link

performMethod:'s responseHandler is not called in NSEventTrackingRunLoopMode #127

Closed ahmetb closed 8 years ago

ahmetb commented 10 years ago

I have an OS X application which is analogous to OS X WiFi networks list status bar which we all use everyday.

It refreshes the list every few seconds and updates the new list in place. This works very well unless I use [NXOAuth2Request performMethod...] and mock the data source. However when I start using performMethod, the responseHandler: block is only invoked when I click somewhere and status menu closes.

I add the timer that invokes my selector (say, mySel) like this:

 [[NSRunLoop currentRunLoop] addTimer:nearbyListRefreshTimer forMode:NSEventTrackingRunLoopMode];

and then I have

-(void)mySel {
    [NXOAuth2Request performMethod:@"GET"
                        onResource:[NSURL URLWithString:@"https://..."]
                        usingParameters:nil
                        withAccount:[self getAuthenticatedAccount]
                        sendProgressHandler:nil
                        responseHandler:^(NSURLResponse *response, NSData *responseData, NSError *error){
                            NSLog("Handler!")
                        }];
    NSLog(@"Started API call.");
}

When I click the menu, I add the timer to run loop and then my selector gets invoked and prints Started API call however Handler! is only printed after menu is closed (e.g. by clicking somewhere else). It's even happening NSMenuDelegate's menuDidClose: is called.

It's never called as long as I keep the menu open. It just keeps invoking my selector and I get bunch of Started API call messages. But the moment it's closed all requests are executed (I see the network+server delay ~100 ms in API call between requests and Handler! is printed, so they're invoked serially when menu is closed.)

I don't know much about run loops and threading in Objective-C, so I tried to provide as much information as I can.

For now I'll start using AFNetworking and will directly use [[account accessToken] accessToken] but I'm not sure that will be refreshed automatically when expired etc and when the NXOAuth2Request refreshes the tokens normally etc. I really appreciate if you can help with this. Thanks!

ahmetb commented 10 years ago

It works if I use AFNetworking by forging the request using [[account accessToken] accessToken]. So there must be an issue with this library.