21 pilots
21 pilots

Reputation: 21

How to handle Realm accessed from incorrect thread?

so there are tons of same question but none of them helped me that's why i'm asking the same question again, so i'm getting this Exception while accessing an Array of RealmObject. when i checked similar question and Realm's official website i found on thing similar in every question

Realm objects are not thread safe and cannot be shared across threads, so you must get a Realm instance in each thread/dispatch queue in which you want to read or write.

so what i did is i created new instances of Realm before accessing it

here's some snippet:

        let realm = try! Realm()
        try! realm.write {
            realm.add(saveUserCredentials, update:  true)
           }

i created a new Instance everytime before accessing realm, something like i stated at above

still i was getting the same error then i tried to access my Realm Instance on only MainThread, i did something like:

if Thread.isMainThread{
    let realm = try! Realm()
    try! realm.write {
        realm.add(saveUserCredentials, update:  true)
     }
 }else{
    DispatchQueue.main.async {
         let realm = try! Realm()
        try! realm.write {
            realm.add(saveUserCredentials, update:  true)
         }
    }}

still i'm getting the same error

fatal error: unexpectedly found nil while unwrapping an Optional value libc++abi.dylib: terminating with uncaught exception of type realm::IncorrectThreadException: Realm accessed from incorrect thread. (lldb)

and i'm pretty sure what i'm doing is not the correct way to do it, and the error is occurring not so often, 1 out of 10 times

anyone can point out what's exactly i have to do for handling this Exception?

Upvotes: 1

Views: 6409

Answers (2)

Jano
Jano

Reputation: 63667

Since your root concern is how to handle threading in Realm, you may be interested in using a value typed api that keeps realm objects inside the boundaries of your persistence layer. There are pros and cons that may fit your use case or not. You could also use both value and realm native types in different parts of a single project depending on your use case.

Upvotes: 0

kishikawa katsumi
kishikawa katsumi

Reputation: 10573

Realm objects are not thread safe and cannot be shared across threads, so you must get a Realm instance in each thread/dispatch queue in which you want to read or write.

Realm objects means Realm, Objects, Results, List and LinkingObjects. saveUserCredentials is instance of Objects or List right? So you cannot pass saveUserCredentials accross threads. To resolve this, you can wrap it by ThreadSafeReference then pass it accross threads, or re-fetch saveUserCredentials each thread as well as realm

See also https://realm.io/docs/swift/latest/#threading

Upvotes: 1

Related Questions