[ad_1]
I need to begin studying to make use of NSAsynchronousFetchRequest by referring to https://www.marcosantadev.com/coredata_crud_concurrency_swift_2/
I’ve the only use case of NSAsynchronousFetchRequest
NSAsynchronousFetchRequest
// Name from UI primary thread
func X() {
let fetchRequest = NSFetchRequest<NSPlainNote>(entityName: "NSPlainNote")
let asynchronousFetchRequest = NSAsynchronousFetchRequest(fetchRequest: fetchRequest) { asynchronousFetchResult in
guard let end result = asynchronousFetchResult.finalResult as? [NSPlainNote] else { return }
}
let coreDataStack = CoreDataStack.INSTANCE
// backgroundContext created through persistentContainer.newBackgroundContext()
let backgroundContext = coreDataStack.backgroundContext
backgroundContext.carry out {
do {
attempt backgroundContext.execute(asynchronousFetchRequest)
} catch let error {
backgroundContext.rollback()
error_log(error)
}
}
}
Nonetheless, operating the above code will get me the next error
CoreData`+[NSManagedObjectContext
Multithreading_Violation_AllThatIsLeftToUsIsHonor]:
If I modify the code by utilizing NSFetchRequest immediately.
NSFetchRequest
// Name from UI primary thread
func X() {
let fetchRequest = NSFetchRequest<NSPlainNote>(entityName: "NSPlainNote")
let coreDataStack = CoreDataStack.INSTANCE
// backgroundContext created through persistentContainer.newBackgroundContext()
let backgroundContext = coreDataStack.backgroundContext
backgroundContext.carry out {
do {
let nsPlainNotes = attempt fetchRequest.execute()
} catch let error {
backgroundContext.rollback()
error_log(error)
}
}
}
Factor works high quality. Might I do know, what’s improper with my NSAsynchronousFetchRequest model of code?
That is my CoreDataStack.swift for reference objective.
CoreDataStack.swift
import CoreData
class CoreDataStack {
static let INSTANCE = CoreDataStack()
non-public init() {
}
non-public(set) lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(title: "wenote")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// It is a severe deadly error. We'll simply merely terminate the app, slightly than utilizing error_log.
fatalError("Unresolved error (error), (error.userInfo)")
}
})
// In order that when backgroundContext write to persistent retailer, container.viewContext will retrieve replace from
// persistent retailer.
container.viewContext.automaticallyMergesChangesFromParent = true
// TODO: Undecided these are required...
//
//container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
//container.viewContext.undoManager = nil
//container.viewContext.shouldDeleteInaccessibleFaults = true
return container
}()
non-public(set) lazy var backgroundContext: NSManagedObjectContext = {
let backgroundContext = persistentContainer.newBackgroundContext()
// Comparable conduct as Android's Room OnConflictStrategy.REPLACE
backgroundContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
// TODO: Undecided these are required...
//backgroundContext.undoManager = nil
return backgroundContext
}()
}
[ad_2]
