Question

I was wonder what is the best way to : Create a NSManagedObject in Edit mode. I was thinking something like

[NSEntityDescription insertNewObjectForEntityForName:nil inManagedObjectContext:managedObjectContext];
[self setEditing:YES animated:YES];

// Start editing the new object name.
WVGenericTableViewCell *cell = (WVGenericTableViewCell *)[self.tableView     cellForRowAtIndexPath:[self.fetchedResultsController objectAtIndexPath:indexPath]];
[cell makeNameFieldFirstResponder];

The above scenario gave me the following problems:

1.The Cell does not becomeFirstResponder 2.The - (BOOL)textFieldShouldEndEditing:(UITextField *)textField crashes my app

Looks Like this

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
//Get IndexPath From TextField
WVGenericTableViewCell *cell =(WVGenericTableViewCell *)   textField.superview.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

Ingredient *newIngredient = [self.fetchedResultsController objectAtIndexPath:indexPath];
newIngredient.name = textField.text;
//Save
[[self coreDataStack] saveManagedObjectContext];

return YES;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}

Any Suggestion or alternatives will be highly appreciated If there is a better more elegant way Please Point IT OUT

EDIT :OK

[NSEntityDescription insertNewObjectForEntityForName:nil inManagedObjectContext:managedObjectContext];

insertNewObjectForEntityForName CANNOT BE nil (crashing problem solved)

Was it helpful?

Solution

A better way would be to create a new NSManagedObjectContext with "main queue" concurrency type and in it perform all changes to your newly created or existing items (edit existing by importing them to the new context).

This way your main context could still save changes on it without committing along with the changes the new object that is only partially populated.
And, you could discard all changes by simply discarding the new context without saving it.

The context could be a child of the main context or report changes via notifications.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top