【发布时间】:2015-11-04 10:52:49
【问题描述】:
我有一个实际版本号为 2.4 的应用程序。从 1.0 版本开始,我在每次更新时都使用应用程序委托中的核心数据。因此,每次更新我都在 1.0 版附带的原始数据库中添加了元素。这是添加元素的方式,例如:
//ADD ELEMENT
if (context == nil) { context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; }
Negozi *newElement = [NSEntityDescription insertNewObjectForEntityForName:@"Elements" inManagedObjectContext:context];
newElement.name=@"element name";
newElement.id=@"101";
[context save:nil];
1.0 附带的 db 包括从 1 到 100 的 newElement.id ,从 newElement.id 101 是在应用程序的较新版本之一中添加的元素。问题来自于 deleteObject。这样我就可以从我的上下文中删除一个对象:
if (context == nil) { context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; }
NSError *error=nil;
///DELETE Element with id 101
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Elements" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
NSString *ElementToDelete=[NSString stringWithFormat:@"%@",@"101"];
for (int i=0; i<[fetchedObjects count]; i++) {
if ([[[fetchedObjects objectAtIndex:i] valueForKey:@"id"] isEqualToString:ElementToDelete]) {
[contesto deleteObject:[fetchedObjects objectAtIndex:i]];
}
}
[contesto save:nil];
使用此代码,我可以成功地从 id 101 中删除对象(随着应用程序的更新添加的对象)。如果我尝试删除一些 id
CoreData: error: failed to resolve optimistic locking failure: optimistic locking failure with (null)
这是我的托管对象上下文代码:
#pragma mark -
#pragma mark Core Data stack
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext_ != nil) {
return managedObjectContext_;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext_ = [[NSManagedObjectContext alloc] init];
[managedObjectContext_ setPersistentStoreCoordinator:coordinator];
}
return managedObjectContext_;
}
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel_ != nil) {
return managedObjectModel_;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Elements" withExtension:@"momd"];
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return managedObjectModel_;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Elements.sqlite"];
NSString *storePath = [[[self applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"Elements.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Elements" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:defaultStorePath]];
}
}
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator_;
}
为什么我不能删除最初存储在 db 中的对象?
【问题讨论】: