Grand Central Dispatch (GCD) 和异步 API
我正在使用 Twitter API 来发布推文。有时这可能需要一些时间,所以我想在后台执行“推文发布”操作。为此,我正在使用 GCD,如下所示:
- (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
});
}
问题是,我没有收到委托回调!我缺少什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试在主线程上运行 Twitter 连接?如果它在主线程上运行而不是在某些后台队列上运行,则您可能会遇到
NSURLConnection
的运行循环问题。 (只是一个疯狂的猜测。)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.)