Ich habe mit Core Data in einer iPad App gearbeitet und kann Daten innerhalb der App erfolgreich speichern und abrufen. Wenn Sie die Anwendung jedoch vollständig schließen, beenden Sie sie, beenden Sie das Multitasking und die Daten verschwinden.CoreData dauerhaft speichern?
Also halten Core-Daten diese Daten irgendwo, wenn die App geschlossen ist? Oder muss ich woanders suchen?
EDIT: Dies ist in der App Delegate didFinishLaunchingWithOptions
: [[[UIApplication sharedApplication] delegate] managedObjectContext];
und dann habe ich dies: context_ = [(prototypeAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
in der UIView-Unterklasse.
Dies ist der NSPersistentStoreCoordinator Code premade im AppDelegate:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"prototype.sqlite"];
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
/*
Replace this implementation 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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator_;
}
Bisher ich dies unter Verwendung von Daten zu holen:
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
NSEntityDescription *testEntity = [NSEntityDescription entityForName:@"DatedText" inManagedObjectContext:context_];
[fetch setEntity:testEntity];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"dateSaved == %@", datePicker.date];
[fetch setPredicate:pred];
NSError *fetchError = nil;
NSArray *fetchedObjs = [context_ executeFetchRequest:fetch error:&fetchError];
if (fetchError != nil) {
NSLog(@"fetchError = %@, details = %@",fetchError,fetchError.userInfo);
}
noteTextView.text = [[fetchedObjs objectAtIndex:0] valueForKey:@"savedText"];
Und diese Daten zu speichern:
NSManagedObject *newDatedText;
newDatedText = [NSEntityDescription insertNewObjectForEntityForName:@"DatedText" inManagedObjectContext:context_];
[newDatedText setValue:noteTextView.text forKey:@"savedText"];
[newDatedText setValue:datePicker.date forKey:@"dateSaved"];
NSError *saveError = nil;
[context_ save:&saveError];
if (saveError != nil) {
NSLog(@"[%@ saveContext] Error saving context: Error = %@, details = %@",[self class], saveError,saveError.userInfo);
}
Wie setzen Sie Ihre managedObjectContext up? Damit die Daten nach dem Beenden gespeichert werden können, müssen Sie einen NSPersistentStoreCoordinator einrichten. – DerekH
Aktualisierte Frage mit mehr Details. –
Ihr Sicherheitscode ist definitiv falsch. Sie dürfen * nicht den Wert von 'saveError' betrachten, es sei denn, der Aufruf von' -save: 'gibt NO zurück. Dies ist ein Programmierfehler und kann Ihre Anwendung zum Absturz bringen. '-save:' kann nicht garantieren, dass diese Variable mit einem gültigen Wert verlassen wird, außer wenn NO zurückgegeben wird. Sie sollten also 'if (! [Context_save: & saveError]) {/ * read saveError * /}' verwenden. –