在编写过程中如何处理Realm的错误? [英] How to handle error with Realm during writing?

查看:145
本文介绍了在编写过程中如何处理Realm的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经使用过SQL数据库,对Realm还是陌生的,到目前为止,这个新的移动数据库的易用性给我留下了深刻的印象. 但是我确实不了解一些事情:如何处理错误抛出?

I'm used to working with SQL database, I'm new to Realm and, so far, I'm really impressed by the ease of use of this new mobile database. But there is something that I really don't undestand: how to handle error throwing?

以这个简单的例子为例:

Take this simple example:

我想在Realm数据库中存储一些市场股票.
每只股票都有一个符号"作为唯一标识符:Apple Inc.的APPL,Tesla Motors Inc.的TSLA等.
我认为将这些符号声明为主键是有道理的,因为在数据库中不可能多次具有相同的符号
当用户单击一个符号(在符号列表中)时,该符号将保存在db中.

I want to store in a Realm DB some market stocks.
Each stock has a "symbol" as a unique identifier: APPL for Apple Inc, TSLA for Tesla Motors Inc, etc.
I believe it would make sense to declare these symbols as primary keys, since it's not possible to have multiple times the same symbol in the database
When user clicks on a symbol (among a list of symbols), this symbol is saved in db.

在此 Raywenderlich教程中,说: 为简化本教程所需的代码,在调用引发错误的Realm方法时,将使用try!.在您自己的代码中,您实际上应该使用try and do/catch来捕获错误并适当地处理它们."

因此,请按照以下模式进行操作:

So according to the following pattern:

do {
    try realm.write {
        realm.add(symbol)
    }
}
catch let error as NSError {
    print("Something went wrong: \(error.localizedDescription)")
}

到目前为止,这是有道理的.
而且,如果用户单击数据库中已经存在的符号,那么我(很逻辑上)会得到一个错误:

So far it makes sense.
And if a user clicks on a symbol that is already in the database, I get (very logically) an error:

*** Terminating app due to uncaught exception 'RLMException', reason: 'Can't set primary key property 'symbol' to existing value 'APPL'.'

问题是在运行时未捕获到此错误:我崩溃了.

The problem is that this error is not catched during run time: I've got a crash instead.

我的问题不是关于如何避免这种崩溃,我当然理解,通过在数据库中写入任何内容之前进行一次简单的测试就很容易避免这种情况的发生:)

My question is not about how to avoid such a crash, I understand of course that it's easy to avoid it by doing a simple testing before writing anything in the database :)

我的问题是我们如何在Realm中捕捉潜在的书写错误?
我在做错什么吗?

My question is how do we do in Realm to catch potential writing errors?
Am I doing something wrong?

推荐答案

do/try/catch捕获Swift错误,这与Objective-C异常完全不同. Realm遵循Foundation的错误报告模式:API滥用错误会引发异常,这些异常不会被捕获(并且不能在Swift中捕获),可恢复的错误会引发Swift错误(或使用NSError出Objective-C中的参数).

do/try/catch in Swift catch Swift Errors, which are an entirely distinct thing from Objective-C exceptions. Realm follows Foundation's pattern for error reporting: API misuse errors throw exceptions which are not intended to be caught (and can't be caught in Swift), and recoverable errors throw Swift errors (or use NSError out parameters in Objective-C).

添加具有重复主键的对象被视为API滥用,因此这是一个致命错误,因为处理"的途径是修复代码中的错误.尝试保存新数据时,可能会导致catch捕获的Swift错误的可恢复错误示例磁盘空间不足.

Adding an object with a duplicate primary key is considered API misuse, so it's a fatal error as the route for "handling" it is to fix the bug in your code. An example of a recoverable error which would produce a Swift error that would be caught by catch is running out of disk space while trying to save the new data.

这篇关于在编写过程中如何处理Realm的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆