简体   繁体   中英

How get Realm result of objects in background queue, then use it on main thread

I'm new in Realm. I have db Reaml and show some data from db to Tableview.

My code:

class MyRealmObject: Object {
@objc dynamic var id = 0
@objc dynamic var name = ""
@objc dynamic var favorite = false}

class MyViewController: UIViewController, ... {
    let realm = try! Realm()
    var objectsResult: Results<MyRealmObject>!
    ...
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    cell.textLabel?.text = objectsResult[indexPath.row].name
    ...

    return cell
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    //searching block
    ...
    DispatchQueue.global().async {
        autoreleasepool{
            let realm = try! Realm()
            var newObjectsResult: Results< MyRealmObject>!
            newObjectsResult = realm.objects(Objects.self).sorted(byKeyPath: "name", ascending: true).filter("name beginswith[cd] %@", searchText)
            objectsResult = newObjectsResult
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    }

}

In this case I got this error:

2018-09-22 14:42:48.262734+0800 TestApp[1194:338211] *** Terminating app due to uncaught exception 'RLMException', reason: 'Realm accessed from incorrect thread.'

How I can use realm objects from background thread to main thread for reloading my tableview?

You need to pass the instances across threads to the main thread. You can do this by using a ThreadSafeReference object. The Realm documentation contains an example:

let person = Person(name: "Jane")
try! realm.write {
    realm.add(person)
}
let personRef = ThreadSafeReference(to: person)
DispatchQueue(label: "background").async {
    autoreleasepool {
        let realm = try! Realm()
        guard let person = realm.resolve(personRef) else {
            return // person was deleted
        }
        try! realm.write {
            person.name = "Jane Doe"
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM