为什么这个Core Data示例崩溃了?

时间:2009-09-03 19:10:49

标签: objective-c core-data entity-relationship

我有一个模型,我有一个Person实体和一个Picture实体。在我的Picture实体中,我有一个属性与Person的关系。

我想有一个关于如何将图片添加到Person的示例,因为我所做的不起作用。

-(BOOL)savePicture:(NSString *)urlPicture:(Person *)person{
SettingsSingleton *userSettings = [SettingsSingleton sharedManager];    
NSManagedObjectContext *managedObjectContext = [userSettings managedObjectContext]; 
NSEntityDescription *myContentEntity = [NSEntityDescription entityForName:@"Pictures" inManagedObjectContext:managedObjectContext];
NSManagedObject *contentToSave = [[NSManagedObject alloc] initWithEntity:myContentEntity insertIntoManagedObjectContext:managedObjectContext];

[contentToSave setValue:urlPicture forKey:@"url"];
[contentToSave setValue:person forKey:@"relatedPerson"];

[managedObjectContext insertObject:contentToSave];  

NSError *error;
if ([managedObjectContext save:&error]){
    //Deal with errors
    NSLog(@"Error");
            return NO; 
}
return YES;
}

如果([managedObjectContext save:& error])它在行上崩溃,并且在控制台中我有这些错误:

-[UITableViewCell objectID]: unrecognized selector sent to instance 0x135c2b0 Serious application error.  Exception was caught during Core Data change processing: ***     
-[UITableViewCell objectID]: unrecognized selector sent to instance 0x135c2b0 with userInfo (null) *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** 
-[UITableViewCell objectID]: unrecognized selector sent to instance 0x135c2b0'

在我之前的观点中,我有以下代码

- (void)viewDidLoad {
[super viewDidLoad];
SettingsSingleton *settings = [SettingsSingleton sharedManager];    
managedObjectContext = [settings managedObjectContext]; 
fetchedResultsController = [settings fetchedResultsController];
fetchedResultsController.delegate=self;

NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
    // Handle the error...
}
[settings release];

self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)  style: UITableViewStyleGrouped];
[table setDataSource:self];
table.delegate=self;
[self.view addSubview: self.table];
}


- (NSFetchedResultsController *)fetchedResultsController {

if (fetchedResultsController != nil) {
    return fetchedResultsController;
}

Person *personTmp=[[Person alloc] init];
personTmp=[personTmp getPersonById:self.personId];

/*
 Set up the fetched results controller.
 */
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Pictures" inManagedObjectContext:managedObjectContext];
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"relatedPerson == %@ AND contentType == %d",personTmp, CONTENT_DATA_PICTURE];

[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

[aFetchedResultsController release];
[fetchRequest release];
[predicate release];
[personTmp release];


return fetchedResultsController;
}    


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(indexPath.row==0){
    cell.textLabel.text=@"Add a picture";
}
else{
    int nb=indexPath.row-1;
    NSIndexPath *ip = [NSIndexPath indexPathForRow: nb inSection:indexPath.section];
    NSManagedObject *managedObject = [fetchedResultsController  objectAtIndexPath:ip];
    cell.textLabel.text = [[managedObject valueForKey:@"url"] description];
    [ip release];
}
return cell;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
int nb=[[fetchedResultsController sections] count];
if (nb == 0) {
    nb = 1;
}
return nb;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSArray *sections = [fetchedResultsController sections];
NSUInteger count = 0;
if ([sections count]) {
    id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];
    count = [sectionInfo numberOfObjects];
}
return count+1;
}

2 个答案:

答案 0 :(得分:6)

我发生了非常类似的错误。我有一个navController首先加载customTypeA viewController,然后加载customTypeB viewController。这两个viewControllers都使用Core Data和fetchedResultsControllers作为tableview的数据源。 customTypeB的功能是向customTypeA的数据源添加新的数据对象。这样做,我会收到您在上面说明的错误“Serious Error in Core Data, Unrecognized Selector. EXC_BAD_ACCESS”;但是我的错误表明无法识别的选择器是“controllerWillChangeContent”。我的两个viewControllers都使用了fetchedViewController委托方法“controllerWillChangeContent,controllerDidChangeObject ...,controllerDidChangeContent”。

我注意到当viewControllerB更改了controllerA的数据时,即使controllerB尚未“弹出”,controllerA的fetchedResultsController也会被激活。所以我想可能是控制器B的委托被调用了。查看Core Data编程文档,我看到他们建议使用fetchedResultsController为每个视图,你应该实现viewWillDisappear并在其中设置fetchedResultsController = nil;对我来说,这解决了我的问题。

另外FYI - 在为Snow Leopard安装XCODE 3.2之前,我没有遇到过fetchedResultsControllers调用不可见viewControllers的委托的问题。代码在Leopard的Xcode3.1中运行良好。

答案 1 :(得分:1)

首先,您不必调用'insertObject'。来自苹果documentation

  

如果context不是nil,则此方法调用[context insertObject:self](这会导致调用awakeFromInsert)。

所以你冗余地插入对象两次。不确定这可能带来什么负面影响,但它不会很好。

至于错误,看起来代码的其他部分可能是罪魁祸首。该消息的含义基本上是您尝试访问UITableViewCell的CoreData objectID。由于这是在保存期间发生的,我想在这个函数之前的某个时刻,你可能会将一个UITableViewCell插入到managedObjectContext中,然后当你真正继续保存它时,你只能在这里吹灭。