Estoy en el proceso de intentar mover el código de una clase UITableViewController a una clase "auxiliar".¿Cómo devolver un objeto de una clase que utiliza NSURLConnection y sus clases delegadas?
El código utiliza NSURLConnection para capturar y analizar JSON y luego llenar un NSMutableArray.
Lo que me gustaría hacer es llamar a un método en mi clase de ayuda que devuelve un NSMutableArray. Lo que no entiendo es cómo devolver la matriz de la clase delegada connectionDidFinishLoading de NSURLConnection (donde la matriz está realmente construida) como si fuera del método originalmente llamado que inició la conexión. En otras palabras, ¿cómo recupera el control el método que llama a NSURLConnection para que pueda devolver un valor de toda la operación?
Aquí están los métodos relevantes de la clase auxiliar. ¿Cómo obtengo el método getMovies para devolver el listOfMovies creado en la clase de delegado connectionDidFinishLoading?
-(NSMutableArray)getMovies:(NSURL*)url {
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//NSURLRequest* request = [NSURLRequest requestWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 30.0];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//TODO error handling for connection
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//---initialize the array---
listOfMovies = [[NSMutableArray alloc] init];
tmdbMovies = [[NSArray alloc] init];
posters = [[NSArray alloc] init];
thumbs = [[NSDictionary alloc] init];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
SBJsonParser *json = [[SBJsonParser new] autorelease];
tmdbMovies = [json objectWithString:responseString];
// loop through all the top level elements in JSON
for (id movie in tmdbMovies) {
// 0 - Name
// 1 - Meta
// 2 - Url
if ((NSNull *)[movie objectForKey:@"name"] != [NSNull null]) {
if (![[movie objectForKey:@"name"] isEqualToString:@""]) {
name = [movie objectForKey:@"name"];
}
}
if ((NSNull *)[movie objectForKey:@"info"] != [NSNull null]) {
if (![[movie objectForKey:@"info"] isEqualToString:@""]) {
meta = [movie objectForKey:@"info"];
}
}
if ((NSNull *)[movie objectForKey:@"thumb"] != [NSNull null]) {
if (![[movie objectForKey:@"thumb"] isEqualToString:@""]) {
thumbUrl = [movie objectForKey:@"thumb"];
}
}
NSLog(@"Name: %@", name);
NSLog(@"Info: %@", meta);
NSLog(@"Thumb: %@", thumbUrl);
NSMutableArray *movieData = [[NSMutableArray alloc] initWithObjects:name,meta,thumbUrl,nil];
// add movieData array to listOfJMovies array
[listOfMovies addObject:movieData];
[movieData release];
}
//FIXME: Connection warning
if (connection!=nil) {
[connection release];
}
[responseData release];
[responseString release];
}
De cualquier protocolo de uso o NSNotificationCenter – Vjy