Grand Central Dispatch (GCD) and asynchronous APIs
문제
I'm using Twitter API to post tweets. At times this can take some time, so i want to perform the "Tweet posting" operation in the background. For that i'm using GCD, like this:
- (void)myClassMethodToPostTweet {
dispatch_async(network_queue, ^{
// … construct the tweet message
NSString *tweet = @"…";
// … check if network is available
[self isConnectedToWeb];
// … initialize twitter API
TwitterAPIClass *twitterAPI = [[[TwitterAPIClass alloc] init…] autorelease];
twitterAPI.delegate = self;
twitterAPI.APIKey = ...;
twitterAPI.APISecret = ...;
// … use twitter API to post the tweet
[twitterAPI postTweet:tweet];
});
}
...
/* and when the API reports a successful operation, update the required variables and UI */
...
- (void)twitterAPIDelegateMethodReportingOperationSuccess {
// … update any variables/records
// … update UI
dispatch_async(dispatch_get_main_queue(), ^{
// … UI updation code
});
}
The problem is, I'm not getting the delegate callback! What am i missing?
해결책
Did you try running the Twitter connection on the main thread? If it works on the main thread and not on some background queue, you might have run into run loop issues with NSURLConnection
. (Just a wild guess.)
제휴하지 않습니다 StackOverflow