There is something I dont understand in core data.
I have created a NSManagedDocument, made a change, and saved it.
Then I created another one and opened the file I saved.
From what I understand the NSManagedDocument should have the change I mad, but it doesn’t.
here is the code I wrote:
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"Data Base"];
// url is now "<Documents Directory>/Default Photo Database"
self.dataBase = [[UIManagedDocument alloc] initWithFileURL:url];
if (self.dataBase.documentState == UIDocumentStateClosed) {
[self.dataBase openWithCompletionHandler:^(BOOL success) {
}];
}
Position *pos = [NSEntityDescription insertNewObjectForEntityForName:@"Position" inManagedObjectContext:self.dataBase.managedObjectContext];
pos.name = @"positon 1";
[self.dataBase saveToURL:self.dataBase.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success){
}];
self.dataBase = [[UIManagedDocument alloc] initWithFileURL:url];
if (self.dataBase.documentState == UIDocumentStateClosed) {
[self.dataBase openWithCompletionHandler:^(BOOL success) {
}];
}
pos = [NSEntityDescription insertNewObjectForEntityForName:@"Position" inManagedObjectContext:self.dataBase.managedObjectContext];
pos.name = @"position 2";
as far as i understand I should have 2 objects for entity “Position” by now, but I only have the last one, why is that?
what am I missing here?
I don’t know why, but as soon as I moved my code to the AppDelegate it was solved…