DispatchQueue.global(qos: .background).async {
RccContactController.shared.updateDbForAppUsers(contactModels: contacts)
}
DispatchQueue.global(qos: .background).async {
RccContactController.shared.updateSyncStatus(lastCount : lastIndex)
DispatchQueue.main.async {
ContactDataStore.shared.updateContacts(withAppUsers: contacts)
if let safeDelegate = RccContactController.shared.delegate {
safeDelegate.syncedPhonebookContact(contacts: restContacts, appUsers: cont)
}
}
}
上面发生了什么
有时我在第二个线程中崩溃。
从com.apple.main.thread(线程1)中入队
我在这里怎么了?
答案 0 :(得分:1)
这样做:
DispatchQueue(label: "background").async {
RccContactController.shared.updateSyncStatus(lastCount : lastIndex)
ContactDataStore.shared.updateContacts(withAppUsers: contacts)
DispatchQueue.main.async {
if let safeDelegate = RccContactController.shared.delegate {
safeDelegate.syncedPhonebookContact(contacts: restContacts, appUsers: cont)
}
}
}
常规示例:
DispatchQueue(label: "background").async {
do {
let realm = try Realm(configuration: config)
let obj = realm.resolve(wrappedObj)
try realm.write {
DispatchQueue.main.async {
//Callback or Update UI in Main thread
}
}
}
catch {
//Callback or Update UI in Main thread
}
}
在DispatchQueue.main.async
中仅执行UI操作,其余操作均保留在后台线程中。