2016-10-07 1 views
0

내 응용 프로그램에는 데이터를 다운로드하여 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)") 
     } 
    } 
} 
+0

핵심 데이터 스택을 어떻게 설정했는지 확인해야합니다. 'save()'는 값을 저장하고'parentContext'에 푸시합니다. 부모 노드가없는 마지막 컨텍스트에'while' 루프로'save()'를 연결해야합니다. 이 컨텍스트는'persistentStoreCoordinator'에 직접 연결됩니다. –

+0

@ New16, 핵심 데이터 스택을 추가했습니다. 'save() '를 어떻게 연결해야합니까? – Jamil

답변

1

그래서 첫 번째는 당신이에 privateContext에서 링크를 만들 필요가 있다는 것입니다 NSPersistentContainerviewContext. 따라서 개인 정보를 생성하는 동안 privateContext, privateContext.parentContext = viewContext. 저장 중 let context = privateContext while(context != nil) { context.save() context = context.privateContext } 이 방법이 효과적입니다. 왜 처음에는 오류가 발생했는지 확신 할 수 없습니다.

+0

동일하지 않습니까? viewContext 인 컨텍스트를 만든 다음 privateContext의 parentContext로 설정합니다. 저장 정보 - 코어 데이터 스택에 추가하거나 모든 코드를 어디에나 바꿔야합니까? – Jamil

+0

'NSManagedObjectContext'의'extension'을 생성하여'save()'메소드의 전파를 포함시킵니다. 그래서'saveAndPropagate()'와 같이'save()'를 호출하는 대신에. –

+0

'context = context.privateContext' 여기'context.parent'를 의미합니까? – Jamil

관련 문제