1

私はTwitter APIを使用してツイートを投稿しています。時にはこれには時間がかかることがあるので、バックグラウンドで「Tweet投稿」操作を実行したい。そのために私はこのような、GCDを使用しています:Grand Central Dispatch(GCD)と非同期API

- (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 
    }); 
} 

問題がある、私は、デリゲートのコールバックが届きません!私は何が欠けていますか?

+0

どのTwitter APIを使用していますか? –

+0

http://github.com/bengottlieb/Twitter-OAuth-iPhone – Mustafa

答えて

2

メインスレッドでTwitter接続を実行しようとしましたか?メインスレッドで動作し、バックグラウンドキューでは動作しない場合は、NSURLConnectionでループ実行の問題が発生している可能性があります。 (ただの野生の推測)

+2

+1実行ループの問題です。 MGTwitterEngineは通信にNSURLConnectionの非同期APIを使用します。これには実行ループが必要です。コードに大きな変更を加えることなく、これをGCDで動作させることは間違いありません。 GCDは、実行ループベースの通信ではうまく動作しません。 –

+0

私は他にどのような選択肢がありますか? NSOperationは仕事をしますか? (しかし、私はそれがGCDをフードの下で使用するので、それを疑う) – Mustafa

+1

Twitterライブラリはあなたに非同期インターフェースを提供していないのですか?明らかに既に非同期の 'NSURLConnection'を使用していることを考えると、自然な選択のようです。あなたが私たちに与えたコードサンプルは、十分に非同期に見えます。それはどこかでブロックされますか? – zoul

関連する問題