问题描述
我试图在NSManagedobjectContext中发现perform和performAndWait之间的区别。 我在不同的线程中使用了不同的上下文。
func addNewCell(name: String,id: String,time: Int64) {
let newContext = NSManagedobjectContext(concurrencyType: .privateQueueConcurrencyType)
newContext.parent = context
let cellEntity = NSEntityDescription.entity(forEntityName: "Cell",in: newContext)
let newCell = Cell(entity: cellEntity!,insertInto: newContext)
newCell.name = name
newCell.id = id
newCell.time = time
do {
try newContext.save()
context.perform {
let t = Int.random(in: 0..<2)
sleep(UInt32(t))
do {
try self.context.save()
}
catch let err {
print(err)
}
}
} catch let err {
print(err)
}}
在上面的代码中,当我在perform和performAndWait之间切换时没什么区别。
func addNewCell(name: String) {
for i in 0..<10 {
dispatchQueue.global(qos: .background).async {
let id = UUID().uuidString
let time = self.currentTime
self.dataSource.addNewCell(
name: name + "\(i)",id: id,time: time
)
}
}
}
上面的代码是在多个线程中调用前一个函数的函数。 我以为使用performAndWait会给出排序的结果,但是没有。
有人可以解释一下perform和performAndWait之间的区别吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)