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?