プライベートコンテキストにいくつかのものを保存して、UIがブロックされないようにしたい。だから、私はすべてがうまくいったと思ったが、私はwifiから自分自身を切断し、アプリを再起動すると、coredataから何も表示されません。だから私はそれが何も保存しないと私は理由を見つけることができないようです。コアデータにデータが保存されない
これは私のコアデータのセットアップです:あなたは私のprivateContextを見ることができると同様
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
}
}
は常に親として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))
}
私はこれを実行する私は無線LANを持っているときNSFetchResultControllerがトリガされるようにアプリが(と思う)それ以外の場合はこれが可能であることができなかったので、私はモデルがデータベース内にあると思います私の結果を示しています。
誰も私が間違っていることを知っていますか?なぜアプリケーションは結果を保存しません(ただし、保存アクションでエラーが発生しない)
チュートリアルが役に立たないようにすべてを実装しました。 NSFetchResultControllerがデータをトリガーして表示しないため、保存しています。私がオフラインになっても表示されないことだけです。 – user1007522