I have an array of objects received via rest response, which I want to insert into realm db in a background thread and use in a uicollectionview in main thread. As soon as I receive response from rest, I call callback function and insert array into db in a background thread. The problem when I am trying to access in main thread property of object being inserted in background I'm getting exception (see below) which I assume because of object not yet inserted
Terminating app due to uncaught exception 'RLMException', reason: 'Realm accessed from incorrect thread.
The model
class User : Object, Mappable {
dynamic var firstName: String?
dynamic var lastName: String?
required convenience init?(map: Map){
self.init()
}
func mapping(map: Map) {
firstName <- map["firstName"]
lastName <- map["lastName"]
}
}
Inserting in a background thread...
DispatchQueue.global().async {
let realm = try! Realm()
try! realm.write {
realm.add(users)
}
}
Rendering in UI...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! UserViewCell
let user = users[indexPath.row]
cell.firstName.text = user.firstName
cell.lastName.text = user.lastName
}
Please note that exception occur either on accessing firstName or lastName.
Please let me know what I'm doing wrong here