Realm accessed from incorrect thread - Swift 3

前端 未结 4 955
借酒劲吻你
借酒劲吻你 2021-01-06 04:37

At the top of my UITableViewController is the following:

let queue = DispatchQueue(label: \"background\")

When a task is dele

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 05:26

    It seems like the write is happening on a different thread than the object was originally accessed from. You should be able to fix it by passing task's id and using that to fetch it from the database right before you do the write (inside the async block).

    So at the top:

    var taskId = 0  // Set this accordingly
    

    and then something like

    self.queue.async {
        autoreleasepool {
            let realm = try! Realm()
            let tempTask = // get task from Realm based on taskId
            realm.beginWrite()
            realm.delete(tempTask)
            do {
                try realm.commitWrite()
            } catch let error {
                self.presentError()
            }
        } 
     }
    

提交回复
热议问题