I tried to find the difference between the perform and performAndWait in NSManagedObjectContext. I used different context in different threads.
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)
}}
In the above code there was no difference when I switch between perform and 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
)
}
}
}
The above code is function that calls previous function in multiple threads. I Thought using performAndWait will give a sorted results but it didn't.
Can someone explain the difference between perform and performAndWait?