如果启用方案“-com.apple.CoreData.ConcurrencyDebug 1”,为什么 raywenderlich 代码会中断
Why does the raywenderlich code break if you enable the scheme "-com. apple.CoreData.ConcurrencyDebug 1"
此示例展示了如何使用 coreData 异步提取数据。如果你的 运行 程序没有方案 "-com.apple.CoreData.ConcurrencyDebug 1" 所有工作,但如果你打开方案 "-com.apple.CoreData.ConcurrencyDebug 1" 我们会得到 运行 时间错误.
为什么会这样我不知道
(我没有违反这些条款)
来自苹果文档
Core Data is designed to work in a multithreaded environment. However, not every object under the Core Data framework is thread safe. To use Core Data in a multithreaded environment, ensure that:
Managed object contexts are bound to the thread (queue) that they are associated with upon initialization.
Managed objects retrieved from a context are bound to the same queue that the context is bound to.
Stack Trace
CoreData`+[NSManagedObjectContext __Multithreading_Violation_AllThatIsLeftToUsIsHonor__]:
0x7fff23a8a65e <+0>: pushq %rbp
0x7fff23a8a65f <+1>: movq %rsp, %rbp
-> 0x7fff23a8a662 <+4>: ud2
override func viewDidLoad() {
super.viewDidLoad()
do {
let batchResult = try coreDataStack.managedContext.execute(batchUpdate) as! NSBatchUpdateResult
print("Records updated \(batchResult.result!)")
} catch let error as NSError {
print("Could not update \(error), \(error.userInfo)")
}
let venueFetchRequest: NSFetchRequest<Venue> = Venue.fetchRequest()
fetchRequest = venueFetchRequest
asyncFetchRequest = NSAsynchronousFetchRequest<Venue>(fetchRequest: venueFetchRequest) {
[unowned self] (result: NSAsynchronousFetchResult) in
guard let venues = result.finalResult else {
return
}
self.venues = venues
self.tableView.reloadData()
}
do {
guard let asyncFetchRequest = asyncFetchRequest else {
return
}
try coreDataStack.managedContext.execute(asyncFetchRequest)
// Returns immediately, cancel here if you want
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
这是一个很好的收获。不过,我希望答案对您有所帮助。
我下载了代码并试用了它,在稍微弄乱了它之后,我很确定这是 Apple 框架中的一个错误。示例项目中的代码看起来是正确的,但并发检查还是对其进行了标记。我不知道这是因为代码实际上导致了并发违规,还是没有违规但标志有问题。
不幸的是,您所看到的只是现在发生的情况,它无法在应用程序中修复——只有 Apple 才能解决。这也是一个长期存在的问题,您可以从 this answer from a few years ago. I suggest filing a bug with Apple.
中看到
此示例展示了如何使用 coreData 异步提取数据。如果你的 运行 程序没有方案 "-com.apple.CoreData.ConcurrencyDebug 1" 所有工作,但如果你打开方案 "-com.apple.CoreData.ConcurrencyDebug 1" 我们会得到 运行 时间错误. 为什么会这样我不知道
(我没有违反这些条款) 来自苹果文档
Core Data is designed to work in a multithreaded environment. However, not every object under the Core Data framework is thread safe. To use Core Data in a multithreaded environment, ensure that:
Managed object contexts are bound to the thread (queue) that they are associated with upon initialization.
Managed objects retrieved from a context are bound to the same queue that the context is bound to.
Stack Trace
CoreData`+[NSManagedObjectContext __Multithreading_Violation_AllThatIsLeftToUsIsHonor__]:
0x7fff23a8a65e <+0>: pushq %rbp
0x7fff23a8a65f <+1>: movq %rsp, %rbp
-> 0x7fff23a8a662 <+4>: ud2
override func viewDidLoad() {
super.viewDidLoad()
do {
let batchResult = try coreDataStack.managedContext.execute(batchUpdate) as! NSBatchUpdateResult
print("Records updated \(batchResult.result!)")
} catch let error as NSError {
print("Could not update \(error), \(error.userInfo)")
}
let venueFetchRequest: NSFetchRequest<Venue> = Venue.fetchRequest()
fetchRequest = venueFetchRequest
asyncFetchRequest = NSAsynchronousFetchRequest<Venue>(fetchRequest: venueFetchRequest) {
[unowned self] (result: NSAsynchronousFetchResult) in
guard let venues = result.finalResult else {
return
}
self.venues = venues
self.tableView.reloadData()
}
do {
guard let asyncFetchRequest = asyncFetchRequest else {
return
}
try coreDataStack.managedContext.execute(asyncFetchRequest)
// Returns immediately, cancel here if you want
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
这是一个很好的收获。不过,我希望答案对您有所帮助。
我下载了代码并试用了它,在稍微弄乱了它之后,我很确定这是 Apple 框架中的一个错误。示例项目中的代码看起来是正确的,但并发检查还是对其进行了标记。我不知道这是因为代码实际上导致了并发违规,还是没有违规但标志有问题。
不幸的是,您所看到的只是现在发生的情况,它无法在应用程序中修复——只有 Apple 才能解决。这也是一个长期存在的问题,您可以从 this answer from a few years ago. I suggest filing a bug with Apple.
中看到