Grand Central Dispatch (GCD) e APIs assíncronas
Pergunta
Estou usando a API do Twitter para postar tweets. Às vezes, isso pode levar algum tempo, então eu quero executar a operação de "postagem de tweet" em segundo plano. Para isso, estou usando o GCD, assim:
- (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
});
}
O problema é que não estou recebendo o retorno de chamada delegado! O que estou perdendo?
Solução
Você tentou executar a conexão do Twitter no tópico principal? Se funcionar no tópico principal e não em alguma fila de fundo, você pode ter encontrado problemas de loop de execução com NSURLConnection
. (Apenas um palpite selvagem.)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow