How to pass parameter in Nsarray in Nsurlsessiondownloadtask url

Asked

Viewed 27 times

0

I am creating a Uitableview to load the bookmarks that are saved in Nsuserdefaults, the values are the Ids. ex. 34, 45, 55...

I am creating the url and passing parameter through a Nsmutablearray. (34, 45, 55). as follows.

    NSString * porGeral = [NSString stringWithFormat:@"http://.../api/listarFavoritos.php?listaCli=%@", @"34, 45, 55"];


    url = [NSURL URLWithString:porGeral];

    NSLog(@"%@", porGeral);

    NSURLSession * session = [NSURLSession sharedSession];

    NSURLSessionDownloadTask * task =
    [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
        NSData * jsonData = [[NSData alloc] initWithContentsOfURL:location];
        news = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"%@", news);
            [self.tableView reloadData];
            [self.progressView removeFromSuperview];

        });
    }];
    [task resume];
}

The error:

*** Terminating app due to uncaught Exception 'Nsinvalidargumentexception', Ason: 'data Parameter is nil'

If I pass this way the url in the browser does not present the error, then I managed to identify that this error is in Nsurlsessiondownloadtask.

Any suggestion?

was worth to all.

1 answer

1

The error occurs when passing a null parameter to the Jsonobjectwithdata method.

Probably the file download is failing and when the completionHandler is called the value of Location is nil.

As good practice, always add treatment for exceptions. This not only prevents crashes, but also helps to identify the root of the problem:

[session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
    if (error) {
        NSLog(@"Error loading URL: %@", error);
        return;
    }

    NSData *data = [[NSData alloc] initWithContentsOfURL:location];
    if (!data) {
        NSLog(@"Cannot create data from URL");
        return;
    }

    NSError *jsonParsingError = nil;
    news = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonParsingError];
    if (jsonParsingError) {
        NSLog(@"Error parsing JSON: %@", jsonParsingError);
        return;
    }

   //... 
}];

If the execution reaches the end of the block, news contains an object, but there is no guarantee that it is array-like since Jsonobjectwithdata: can return other types, such as Nsdictionary.

Another tip, in case it is not really necessary to download, use the dataTaskWithRequest method instead of downloadTaskWithURL, since this already returns an object of type Nsdata.

  • Opa Rafael thanks for the tips, now that I saw what I’m doing with downloadTaskWithURL was using in another application that actually downloads. thanks for the tip.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.