Grand Central Dispatch (GCD) y las API asíncronas
Pregunta
Estoy usando la API de Twitter para publicar tweets. A veces esto puede llevar algún tiempo, así que quiero realizar la operación "Tweet publicación" en el fondo. Para que estoy usando GCD, como esto:
- (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
});
}
El problema es que ahora no recibo la llamada de retorno delegado! ¿Qué me falta?
Solución
¿Usted intentó ejecutar la conexión de Twitter en el hilo principal? Si funciona en el hilo principal y no en una cola en segundo plano, es posible que se han topado con problemas de bucle de ejecución con NSURLConnection
. (Sólo una conjetura salvaje.)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow