1

So I am downloading some data from a server to my ios client. The data needs to be formatted so I use NSNotification to notify the app when the data has been downloaded completely and when thats done, I format the data and then display it on the screen. All this is cool but because of the size of the data, the screen freezes.I thought I should use GCD to push the data downloading to another thread so the UI is still responsive. When i did that, I dont seem to be downloading any data. I have a method getTops that uses NSURLConnection to download the data. Initially, in my viewDidLoad I called this method and it worked fine but then I used GCD like so

dispatch_queue_t getItemsQ = dispatch_queue_create("get Items", NULL);
dispatch_async(getItemsQ, ^{
    [self getTops];
});

And it stopped working. I know it gets to the getTops because I can see the log in the console but it never reaches the -(void)connectionDidFinishLoading:(NSURLConnection *)connection

Here is the code i used:

-(void)getTops{
    Keychain *keychain = [[Keychain alloc]init];
    NSString *auth_token = [keychain getAuthToken];
    NSLog(@"at getTops");
    topimageArray = [[NSMutableArray alloc]init];
    NSURLConnection *connection;

    webData = [[NSMutableData alloc]init];
    NSURL *url = [[NSURL alloc]initWithString:base];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setURL: url];
    [request setValue:auth_token forHTTPHeaderField:@"X-AUTH-TOKEN"];
    connection = [NSURLConnection connectionWithRequest:request delegate:self];
   // [connection start];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    NSLog(@"at getTops conn start");
    }
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"recieved response");
    [webData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    if(data) {
         NSLog(@"Appending data");
         [webData appendData:data];
    }
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSArray *response= [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
    NSLog(@"Tops full response::: %@",response);
    theArray = [[NSArray alloc]initWithArray:response];
    NSLog(@"DONE");
    //Notify that the data is ready to be formated
    [[NSNotificationCenter defaultCenter]postNotificationName:@"getTops" object:nil];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"error::::%@", error);
    NSString *errormsg = error.localizedDescription;
    UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:@"Error" message:errormsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertview show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
}

I thought maybe I should remove the [UIApplication sharedApplication].networkActivityIndicatorVisible but that didnt help

Edit:: Added NSLogs to delegate methods. The Log that I get is

at getTops conn start

And thats it.

nupac
  • 2,459
  • 7
  • 31
  • 56

1 Answers1

3

The simplest way is to use sendAsynchronousRequest:queue:completionHandler:. No delegates are necessary, just put the code from connectionDidFinishLoading: in the completion block.

Loads the data for a URL request and executes a handler block on an operation queue when the request completes or fails.

[NSURLConnection sendAsynchronousRequest:request
   queue:[NSOperationQueue mainQueue]
   completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        < your code >
}];
zaph
  • 111,848
  • 21
  • 189
  • 228