내 응용 프로그램에는 데이터를 다운로드하여 CoreData에 저장하는 작업이 있습니다. 스위프트 3로 마이그레이션 한 후 임의의 시간에 데이터를 저장하는 동안 예외가 발생하기 시작했습니다. 내가 이해했듯이 모든 작업에 대해 하나의 컨텍스트를 사용하면 문제가 발생할 수 있습니다.신속한 3에서 ConcerrencyType을 가진 NSManagedObjectContext를 추가하는 방법?
do{
try privateContext.save()
}
catch{
}
:이 같은 데이터를 저장
static let context : NSManagedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
static let privateContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType)
static func declare()
{
AixmParser.privateContext.parent = AixmParser.context
}
: 지금은 동시성 유형과 다른 상황을 만들어, 내가 컨텍스트를 만드는 방법은 모든 오류없이 작동하지만, 아무것도 파일 : 여기를 .sqlite에 저장되지 않는다
선언이나 핵심 데이터 스택에 무언가를 추가해야합니까?
업데이트 : 아래 핵심 데이터 스택.
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "AppName")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
container.viewContext.perform({
// actions upon the NSMainQueueConcurrencyType NSManagedObjectContext for this container
})
})
return container
}()
// MARK: - Core Data Saving support
func saveContext() {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
핵심 데이터 스택을 어떻게 설정했는지 확인해야합니다. 'save()'는 값을 저장하고'parentContext'에 푸시합니다. 부모 노드가없는 마지막 컨텍스트에'while' 루프로'save()'를 연결해야합니다. 이 컨텍스트는'persistentStoreCoordinator'에 직접 연결됩니다. –
@ New16, 핵심 데이터 스택을 추가했습니다. 'save() '를 어떻게 연결해야합니까? – Jamil