Ich habe ein Problem mit CoreData Concurrency. Ich kann context.perform
nicht tun, während ein Ziel-Thread mit DispatchGroup
blockiert ist.Swift: Deadlock mit DispatchGroup
Hier ist ein einfaches Beispiel, das das Problem zeigt:
func upload(objects: [NSManagedObject]) {
let group = DispatchGroup()
for object in objects {
group.enter()
upload(object) {
group.leave()
}
}
group.wait() // current thread is blocked here
someAdditionalWorkToDoInSameThread()
}
func upload(object: NSManagedObject, completion:()->()) {
let context = object.managedObjectContext
performAlamofireRequest(object) {
context.perform {
// can't reach here because the thread is blocked
update(object)
completion()
}
}
}
Bitte, hilf mir dies richtig neu zu implementieren. Vielen Dank.
Dies sollte es tun;) BTW, seine ursprüngliche 'update' Funktion verwendete einen unnötigen * Completion Block * auf einer eindeutig synchronen Funktion. Macht mir nicht viel Sinn ... –
Ich habe meine erste Funktion aktualisiert, um einen Sinn zu machen. –
@AndriyTrubchanin: Notify hat Ihr Problem nicht gelöst? –