0

I´m having trouble re-saving records in my core data, and i was hopping someone could help me understanding what am i missing here:

When i want to save a record, i have my "product" tableview with the code below:

- (IBAction)save:(id)sender
{

MyCoreDataClass *productOne = [NSEntityDescription insertNewObjectForEntityForName:@"MyCoreDataClass" inManagedObjectContext:self.managedObjectContext];

productOne.reference = reference.text;
productOne.type = type.text;
productOne.cost = cost.text;


[self.managedObjectContext save:nil];

[self dismissModalViewControllerAnimated:YES];

}

As you already understood i have a "Product List" tableview, where all the records are being displayed/saved nicely, and in my "Product List" didSelectRowAtIndexPath method i have done this:(i´m not using segues!)

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCoreDataClass *produtoSelected = (Lista *)[[self fetchedResultsController] objectAtIndexPath:indexPath];

if ([produtoSelected.reference isEqualToString:@"shirts"]||[produtoSelected.reference isEqualToString:@"pants"] )
{
  Product *example=(Product *)[self.storyboard instantiateViewControllerWithIdentifier:@"ProductId"];
    produto.modalPresentationStyle = UIModalPresentationFormSheet;
    produto.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:example animated:YES];
    produto.view.superview.frame = CGRectMake(0, 0, 350, 510); //do this after presentModalViewController
    example.view.superview.center = self.view.center;
    example.reference.text = produtoSelected.reference;
    example.type.text = produtoSelected.type;
    example.cost.text = produtoSelected.cost;
    example.managedObjectContext = self.managedObjectContext;

    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];  
}

The previous saved information is displayed, after that I already tried the "self.managedObjectContext refreshObject" and i know that my last line is not correct...The delegate of my "Product" tableview is my "Product List" tableview...and i have seen examples where the user enters in EDIT mode, but i just don´t want that...i just want the user to press in the desire row, modify what he wants and hit the save button...

What am i missing, i´m losing hours here...could anyone help me?

Thanks in advance for you time.

Japa
  • 632
  • 7
  • 30

1 Answers1

0

First point. The following line is inconsistent:

X *object = (Y*) [fetchedResultsController objectAtIndexPath:indexPath]; 

X should be the class you need, (Y*) you can omit, as the correct object type should be returned automatically.

Second. After your new entity attributes are saved there are two ways to display the changes.

  1. You can simply call [tableView reloadData]. If you do it before the parent view controller appears, it does not even look ugly - but it could be an expensive operation.

  2. Better, you implement the NSFetchedResultsControllerDelegate methods that alerts the view controller to changes in the managed objects. There you simply update the appropriate rows of your table view.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Hello Mundi, thank you for answering. Yes you´re right, i don´t have the need to use (y*). But my X should be my "product" tableview?.Also, i have the NSFetchedResultsControllerDelegate in my "product" tableview, and i´m saying to my "Product List" that she is the delegate of my "product", after this i have all the methods that core data requires(based on my researches and apple core data material). But i know i´m missing some knowledge, could you explain better to me please?. – Japa Nov 17 '12 at 19:34
  • Sorry, you are way off. The object (X) is a subclass of `NSManagedObject`, not some table view. A "list" cannot be the delegate of some "product". Your controller can be the delegate of your fetched results controller (which might display a list of products). All clear. – Mundi Nov 17 '12 at 20:05
  • Understood about the X matter. When i say "product list" i´m referring to a tableview that displays all the products that are inserted by another tableview ("product" wich has the save button) should i be changing my didselectrow in "product list" or paying more attention to my save method in "product"?...sorry to bother you again. – Japa Nov 17 '12 at 20:33