我有一個模型,我有一個Person實體和一個Picture實體。在我的圖片實體中,我有一個與Person關係的屬性。這個Core Data示例爲什麼會崩潰?
我想有一個關於如何將圖片添加到人的例子,因爲我所做的不起作用。
-(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 2009-09-04 21:20:52
您的謂詞過度釋放,可能還有personTmp(取決於getPersonById是否返回自動釋放對象)。崩潰來自這些過度釋放的對象。您應該閱讀Objective-C中的內存管理(Google「Objective-c內存管理」,應該是第一個結果)。此外,如果getPersonById返回一個不同的人物對象,那麼你也正在泄漏你分配的那個對象 - 在此之上。 – bobDevil 2009-09-04 22:49:00
哦是的,現在的作品 但我必須釋放謂詞變量無論如何,我必須使它成爲該類的全局變量,並釋放它在 - (void)dealloc – Mathieu 2009-09-05 00:07:25