我正在从我的库中调用一个函数,该函数应该更新应用程序中的某些UI:
DispatchQueue.global().async {
autoreleasepool {
print("is main thread =", Thread.isMainThread)
for i in 0..<data.count {
for j in 0..<data[i].createArray.count {
let dataObject = NSEntityDescription.insertNewObject(forEntityName: entity, into: context)
// Parse the dictionary of keys/values returned from the synced array into an easily readable JSON file.
let parsedData = json(from: data[i].createArray[j])
DispatchQueue.main.async {
label?.text = "Syncing " + String(describing: j + 1) + " " + entity + "s out of " + String(describing: data[i].createArray.count)
print("is main thread =", Thread.isMainThread)
print(label?.text)
}
}
}
}
}
但是,main.async调用中的块仅在其余函数通过后才执行。我该如何更改它以便在每次循环迭代时进行更新?
答案 0 :(得分:-1)
代替
DispatchQueue.main.async {}
使用
DispatchQueue.main.sync {}