Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm Swift: how to catch RLMException?

Tags:

ios

swift

realm

I thought I was doing it correctly.

let realm = try! Realm()
do {
    try realm.write {
        realm.add(myObject)
    }
} catch {
    print("something went wrong!")
}

But I'm still getting a crash instead of that print statement. I'm not interested in avoiding the exception (in this case I caused it deliberately by adding an object with an existing primary key) but I want to be able to catch it and prevent a crash no matter what. Is this possible, and if so, how?

like image 858
TimSim Avatar asked May 03 '16 20:05

TimSim


1 Answers

Realm Swift throws Objective-C exceptions only for things that are considered to be programmer error. These exceptions are not intended to be caught and handled at runtime as they're indicative of an error in the program that must be fixed.

like image 76
bdash Avatar answered Nov 14 '22 03:11

bdash