グランドセントラルディスパッチ(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
});
}
問題は、私がデリゲートコールバックを取得していないことです!何が足りないの?
解決
メインスレッドでTwitter接続を実行してみましたか?バックグラウンドキューではなくメインスレッドで動作する場合、Run Loopの問題に遭遇した可能性があります NSURLConnection
. 。 (ただの推測です。)
所属していません StackOverflow