Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSManagedObjectContext中perform和PerformdWait之间的差异_Ios_Multithreading_Macos_Core Data_Nsmanagedobjectcontext - Fatal编程技术网

Ios NSManagedObjectContext中perform和PerformdWait之间的差异

Ios NSManagedObjectContext中perform和PerformdWait之间的差异,ios,multithreading,macos,core-data,nsmanagedobjectcontext,Ios,Multithreading,Macos,Core Data,Nsmanagedobjectcontext,我试图在NSManagedObjectContext中找到perform和performAndWait之间的区别。 我在不同的线程中使用了不同的上下文 func addNewCell(name: String, id: String, time: Int64) { let newContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType) newCon

我试图在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)
            }}
func addNewCell(名称:String,id:String,时间:Int64){
让newContext=NSManagedObjectContext(并发类型:.privateQueueConcurrencyType)
newContext.parent=上下文
让cellEntity=NSEntityDescription.entity(在:newContext中,名为“Cell”)
让newCell=Cell(实体:cellEntity!,insertInto:newContext)
newCell.name=名称
newCell.id=id
newCell.time=时间
做{
尝试newContext.save()
context.perform{

让t=Int.random(in:0..您需要查看Apple文档()。区别在于并发性和了解执行的操作顺序。看看这是否有帮助:问题和答案使用的是Objective-C,因此API看起来有点不同(performBlock vs perform,等等)但核心数据的基本概念是相同的。
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
            )
        }
    }

}