Frage

I need to display the error in a NJSONSerialization call in a GCD Block. I want to create an alert, telling the user to check their internet connection and then the error code :

here is some of my code :

this is to step up the block thats not on the main thread :

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)

then I have this

NSMutableDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error: &error];

now i guess the error is stored in &error but how can i display an error in an alert in that block ?

some code would be brilliant.

thanks

War es hilfreich?

Lösung

You'd do something like this...

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
    NSError *error = nil;

    NSMutableDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error: &error];

    if (error) {
        // you have to show the alert on the main thread
        dispatch_async(dispatch_get_main_queue, ^(void) {
            [[[UIAlertView alloc] initWithTitle:@"Error" message:error.userInfo delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        });
    }
});

You'll probably want to display your own message based on the error instead of just showing the error.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top