在 Restkit 中处理 NSAsserts 抛出
我正在使用 Restkit 的对象管理器来处理大部分远程 API 调用。
它会因各种错误而引发 NSAssert。例如,如果服务器返回错误页面而不是格式良好的 JSON,即使代码没有任何问题,它也会引发 NSAssert。
有一些事情我很困惑(其中大部分与异常和 nsassert 的一般处理有关)。
我们应该如何处理这些 NSAsserts 错误?例如,我们希望重试几次,然后显示“出了问题”消息(而不是使应用程序崩溃)。
我尝试使用 catch-try 块来捕获错误(代码如下),但错误没有被捕获。所以我的应用程序总是失败。此外,无论如何,我不习惯在发布模式下使用 try-catch。
仅供我理解,为什么 Restkit 使用 NSAsserts,而不是其他失败?
代码:
// code to catch NSAssert that sharedManager throws
@try{
[sharedManager loadObjectsAtResourcePath:self.resourcePath delegate:self];
}
@catch (NSException *ex) {
NSLog(@"exception caught");
}
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).
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).
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.
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,您不应该尝试捕获 NSAssert 错误,因为它们意味着出现了严重错误 - 例如应用程序内部状态变得不一致,您错误地使用了库等。 , - 因此应用程序需要退出。[1]
错误未被捕获的原因是
NSAssert
引发了NSInternalInconsistencyException
[2],它是一个字符串,而不是 NSException 的实例。您仍然可以按照[3]捕获它们,例如使用,但由于上面列出的原因,不建议这样做。
要回答您的第三个问题,请提供有关引发
NSAssert
的更多详细信息等。[1] NSAssert 实际上有什么意义?
[2] http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/macro/NSAssert
[ 3] <一href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Exceptions/Tasks/HandlingExceptions.html" rel="nofollow noreferrer">http://developer.apple.com/library /mac/#documentation/Cocoa/Conceptual/Exceptions/Tasks/HandlingExceptions.html
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 anNSInternalInconsistencyException
[2], which is a string and not an instance of NSException. You can still catch them as per[3], e.g. withbut 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