私はPersonエンティティとPictureエンティティを持つモデルを持っています。 My Pictureエンティティでは、Personとの関係であるプロパティがあります。このコアデータの例がクラッシュするのはなぜですか?
私が行ったことが機能しないため、Pictureを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保存:&エラー])と、コンソールに、私はこれらのエラーを持っている:
-[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){
[email protected]"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;
}
- (NSFetchedResultsController *)fetchedResultsControllerメソッドのコードにコメントするときに違うエラーが発生しました。私の画像を正しく追加できます:プログラム受信信号: "EXC_BAD_ACCESS"。 – Mathieu
あなたは述語を超過しており、おそらくはpersonTmpです(getPersonByIdが自動解放されたオブジェクトを返すかどうかによって異なります)。クラッシュは、これらの過剰にリリースされたオブジェクトのいずれかからのものです。 Objective-Cのメモリ管理(Googleの "Objective-c memory management"、最初の結果であるはずです)を読んでください。さらに、getPersonByIdが別の人物オブジェクトを返した場合、あなたはその上にalloc-init'dを漏らしています。 – bobDevil
ああ、それは今では で動作しますが、私はそれをクラスのグローバル変数にして、 - (void)deallocで解放する必要がある場合は、述語変数を解放しなければならない – Mathieu