我正在尝试将 NSData 对象插入 CoreData,因此我不得不在事后将所有内容添加到我的应用程序中,因为我从未在选择 coredata 的情况下启动它。没关系,因为我已经开始了一个新项目并复制了所有适当的代码以进行所有设置。
从那里我现在正在尝试设置 insertNewObject 方法。然而,这给了我一些问题。
我首先尝试从像这样的另一种方法传递一些 NSData
[self insertNewObject:myData];
然后我使用这样的 insertNewObject 方法
- (void)insertNewObject:(id)sender
{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:context];
// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
[newManagedObject setValue:sender forKey:@"manufacturers"]; //not sure if this is correct, but sender has myData, and @"manufactures" is the attribute of my entity.
// Save the context.
NSError *error = nil;
if (![context save:&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.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
从这里发生的事情是,当线程太第三行“NSManagedObject *newManagedObject ...”时,应用程序崩溃了,我收到了这个错误
> 2012-04-27 11:18:21.579 thecode[1452:fb03] *** Terminating app due to
> uncaught exception 'NSInternalInconsistencyException', reason:
> '+entityForName: could not locate an NSManagedObjectModel for entity
> name 'Entity''
> *** First throw call stack: (0x14b5022 0x1952cd6 0x61d47 0xa8102 0x1a595 0x199a4 0x18b99 0x15097 0x1136a49 0x1134e84 0x1135ea7
> 0x1134e3f 0x1134fc5 0x1079f5a 0x1c02a39 0x1ccf596 0x1bf9120 0x1ccf117
> 0x1bf8fbf 0x148994f 0x13ecb43 0x13ec424 0x13ebd84 0x13ebc9b 0x215b7d8
> 0x215b88a 0x733626 0x762d 0x1c75 0x1) terminate called throwing an
> exception
我已经在我的 xcdatamodeld 中设置了所需的实体和属性
任何帮助将不胜感激。