2016-07-13 173 views
-1

我想将一些内容保存到私有上下文中,以便我的UI不会被阻止。所以我认为一切正常,但当我断开自己的WiFi并重新启动应用程序,它不显示任何coredata。所以在我看来,它没有保存任何东西,我也不知道为什么。核心数据不会保存数据

这是我的核心数据设置:

class CoreDataDatabaseService: NSObject { 
    // Singleton 
    static let sharedInstance = CoreDataDatabaseService() 

    // MARK: - Core Data stack 

    lazy var applicationDocumentsDirectory: NSURL = { 
     // The directory the application uses to store the Core Data store file. This code uses a directory named "be.donpironet.Gins4u" in the application's documents Application Support directory. 
     let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) 
     return urls[urls.count-1] 
    }() 

    lazy var managedObjectModel: NSManagedObjectModel = { 
     // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model. 
     let modelURL = NSBundle.mainBundle().URLForResource("Model", withExtension: "momd")! 
     return NSManagedObjectModel(contentsOfURL: modelURL)! 
    }() 

    lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { 
     // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added 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. 
     // Create the coordinator and store 
     let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) 
     let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Model.sqlite") 
     var failureReason = "There was an error creating or loading the application's saved data." 
     do { 
      try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil) 
     } catch { 
      // Report any error we got. 
      var dict = [String: AnyObject]() 
      dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" 
      dict[NSLocalizedFailureReasonErrorKey] = failureReason 

      dict[NSUnderlyingErrorKey] = error as NSError 
      let wrappedError = NSError(domain: "G_COREDATA", code: 9999, userInfo: dict) 
      // Replace this with code to handle the error appropriately. 
      // abort() 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. 
      DDLogError("Unresolved error \(wrappedError), \(wrappedError.userInfo)") 
      abort() 
     } 

     return coordinator 
    }() 

    lazy var mainContext: NSManagedObjectContext = { 
     // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail. 
     let coordinator = self.persistentStoreCoordinator 
     var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) 
     managedObjectContext.persistentStoreCoordinator = coordinator 

     return managedObjectContext 
    }() 

    func privateContext() -> NSManagedObjectContext { 
     let managedObjectContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType) 
     managedObjectContext.parentContext = self.mainContext 

     return managedObjectContext 
    } 
} 

就像你可以看到我的privateContext总是有mainContext如父。我只将事情保存在我的私人环境中,而我的主要环境仅显示数据。

let managedContext = CoreDataDatabaseService.sharedInstance.privateContext() 

... I create some objects 

    do { 
        if managedContext.hasChanges { 
         try managedContext.save() 
         observer.on(.Next("Success")) 
         observer.on(.Completed) 
        } 

       } catch let error as NSError { 
        DDLogError("Could not save \(error), \(error.userInfo)") 
        observer.on(.Error(error)) 
       } 

当我运行这个时,我有wifi应用程序显示我的结果让NSFetchResultController被触发,所以我想该机型在数据库中,否则这可能是不可能的(我认为)。

因此,任何人都知道我在做什么错了?为什么应用程序不保存结果(但保存操作不会引发错误)

+0

我实现了一切,所以教程没有帮助。它正在保存,否则NSFetchResultController将不会触发和显示数据。这只是它在我离线时不显示。 – user1007522

回答

0

我发现了这个问题。保存子上下文时,还需要保存父上下文。

所以在保存私有上下文后,只需在maincontext上调用save(),一切正常。