SebT SebT - 1 year ago 277
Swift Question

How to handle error with Realm during writing?

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:

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 makes 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.

In this Raywenderlich tutorial, it is said: "To simplify the code required in this tutorial, you’ll be used try! when calling Realm methods that throw an error. In your own code, you should really be using try and do / catch to catch errors and handle them appropriately."

So according to the following pattern:

do {
try realm.write {
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 :)

My question is how do we do in Realm to catch potential writing errors?

Am I doing something wrong?

Answer Source

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).

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download