I have a model where I have a Person entity and a Picture entity. In my Picture entity I have a property that is a relationship to a Person.
I would like to have an example about how to add a Picture to a Person because what I did doesn't work.
-(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;
}
It crashes on the line if ([managedObjectContext save:&error]) and in the console I have those errors :
-[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'
and in my previous view I have the following code
- (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;
}