Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling NSAsserts in Restkit throws

I am using Restkit's object manager to handle a good chunk of my remote API calls.

It throws NSAssert for a wide range of errors. For example, if the server returns a error page as opposed to well-formed JSON, it will raise an NSAssert, even if there is nothing wrong with the code.

There are a few things I am confused about (most of which has to do with general handling of exceptions and nsasserts).

  1. How should we handle these NSAsserts errors? For example, we would want to retry again for a few times, then show a "something went wrong" message (as opposed to crashing the application).

  2. I tried to use a catch-try block to catch the errors (code below), but the errors are not being caught. So my app just keeps failing. Furthermore, I am not comfortable using try-catch in release mode anyway.

  3. Just for my understanding, why do Restkit use NSAsserts, as opposed to other failure?

Code:

// code to catch NSAssert that sharedManager throws
@try{
    [sharedManager loadObjectsAtResourcePath:self.resourcePath delegate:self];
}

@catch (NSException *ex) {
    NSLog(@"exception caught");
}
like image 609
meow Avatar asked Oct 11 '22 17:10

meow


1 Answers

In general, you should NOT try to catch NSAssert errors, as they mean that something went horribly wrong - e.g. an application internal state become inconsistent, your are using library incorrectly etc., - and so application needs to quit.[1]

The reason your errors are not being caught is because NSAssert raises an NSInternalInconsistencyException[2], which is a string and not an instance of NSException. You can still catch them as per[3], e.g. with

    @catch (id ex)

but it's not recommended for the reasons listed above.

To answer your 3rd question, please provide more details around which NSAssert is raised etc.

[1] What's the point of NSAssert, actually?

[2] http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/macro/NSAssert

[3] http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Exceptions/Tasks/HandlingExceptions.html

like image 98
Victor K. Avatar answered Oct 14 '22 01:10

Victor K.