简体   繁体   中英

How to handle NSJSONSerialization's crashing when disconnected to internet

I implement web service in my app. My way is typical.

- (BOOL)application:(UIApplication *)application     didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Web Service  xxx,yyy are not true data
    NSString *urlString =   @"http://xxx.byethost17.com/yyy";
    NSURL *url = [NSURL URLWithString:urlString];
    dispatch_async(kBackGroudQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: url];
        [self performSelectorOnMainThread:@selector(receiveLatest:)     withObject:data waitUntilDone:YES];
    });   
    return YES;
}

- (void)receiveLatest:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData
                      options:kNilOptions
                      error:&error];
    NSString *Draw_539 = [json objectForKey:@"Draw_539"];
....

console error message:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

When my iphone has connection to Internet, the app works successfully. But if it disconnects to Internet, app will crash on NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; Can you show me how to handle this error? Is NSError helpful?

The error is telling you that "responseData" is nil. The way to avoid the exception is to test "responseData" and not invoke JSONObjectWithData if it's nil. Instead react however you feel you should for this error condition.

You are not checking if your responseData is nil or not, before passing it to the JSONObjectWithData:options:error: method.

Probably you should try this:

- (void)receiveLatest:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    if(responseData != nil)
    {
         NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData
                      options:kNilOptions
                      error:&error];
         NSString *Draw_539 = [json objectForKey:@"Draw_539"];
    }
    else
    {
         //Handle error or alert user here
    }
    ....
}

EDIT-1: For good practice, you should check this error object after JSONObjectWithData:options:error: method to check and see if JSON data is successfully converted to NSDictionary or not

- (void)receiveLatest:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    if(responseData != nil)
    {
         NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData
                      options:kNilOptions
                      error:&error];
         if(!error)
         {
              NSString *Draw_539 = [json objectForKey:@"Draw_539"];
         }
         else
         {
              NSLog(@"Error: %@", [error localizedDescription]);
              //Do additional data manipulation or handling work here.
         } 
    }
    else
    {
         //Handle error or alert user here
    }
    ....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM