Create and edit objects in Core Data - 3 different approaches by some of the most famous books

I read some save messages with basic data. I noticed that most of the time everyone forgot to believe that the most difficult part is editing objects, not creating them.

I read 3 different books about basic data, I will share with you all the methods that these books use. and I would be interested to know which approach you would use for your application.



IOS Essentials Master Data - Packt Publishing

theres RootVC, AddCustomerVC and EditCustomerVC. we want to create or edit a client


1) the user presses the ADD button in RootVC
2) the add button method creates an object and sets it as the Object * myobject variable of the DetailVC element

-(IBAction) addCustomer { AddCustomerVC *addVC = [AddCustomerVC alloc]init]; addVC.customer = (Customer *) [NSEntityDescription insertNewObjectForEntityForName:@"Customer" inManagedObjectContext:self.managedObjectContext]; addVC.delegate = self; [self.navigationController presentModalViewController:addVC animated:YES]; } 


3) detailVC sets the attributes of the Customer instance and calls the delegate method

 -(IBAction) save { customer.name = newName.text; customer.surname = newSurname.text; [delegate addVC:self selectedSave:YES]; } 


4) if the user clicks Cancel in addVC, the actions call the delegate method

 -(IBAction) cancel { [delegate addVC:self selectedSave:NO]; } 


5) RootVC delegate implementation checks if the user has saved and the context has been saved

 -(void) addVC:(UIViewController *)controller selectedSave:(BOOL)save { if(!save) { [self.managedObjectContext deleteObject:controller.customer]; } NSError *error = nil; if( ! [self.managedObjectContext save:&error] ) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); } [self dismissModalViewControllerAnimated:YES]; } 


1 edit) the user clicks on the cell and calls the cell selection method, where he creates a client with the selected value, and open EditVC

 -(void)tablewView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath { EditCustomerVC editVC = [[EditCustomerVC alloc]init]; Customer *selectedCustomer = (Customer *) [self.fetchedResultsController objectAtIndexPath:indexPath]; editVC.customer = selectedCustomer; [self.navigationController pushViewController:editVC animated:YES]; } 


2) the user clicks the save button, sets the client values ​​and calls the delegate

 -(IBAction) save { customer.name = name.text; customer.surname = surname.text; [delegate editVC:self selectedSave:YES]; } 


3b), if the user clicks cancel, he calls the delegate

 -(IBAction) cancel { [delegate editVC:self selectedSave:NO]; } 


4b) the delegate saves the edited object

 .(void)editVC:(UIViewController *)controller selectedSave:(BOOL)save { if(save) { NSError *error = nil; if( ! [self.managedObjectContext save:&error] ) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); } [self dismissModalViewControllerAnimated:YES]; } 



IOS Pro Basic Data - Apress

We have MasterVC and TeamVC, where we can create and edit a team


1) when the user presses the ADD button, he calls showTeamVC, passing him MasterVC and the command

 -(void)showTeamVC { TeamVC *teamVC = [[TeamVC alloc]initWithController:self andTeam:nil]; [self presentModalViewController:teamVC animated:YES]; } 


2) init TeamVC sets the passed value as its iVar

 -(id)initWithController:(UIViewController *)controller andTeam:(Team *)team { self.controller = controller; self.team = team; } 


3) viewDidLoad sets the view fields with the value that it received from the object
4) when the user clicks on cancel, the controller simply quits
5) when the user clicks the "Save" button, he calls the save action, which checks if the nil command is there (to find out if you passed the command or not, so if you add or edit) and calls the save or insert main controller method

 -(IBAction)save { if ( team != nil ){ team.name = nameTextField.text; team.colors = colorsTextField.text; [controller saveContext]; } else { [controller insertTeamWithName: nameTextField.text andColors:colorsTextField.text]; } [self dismissModalViewControllerAnimated:YES]; } 


6) if you saved a new command, it will be called the insertTeamWithName: andColors method in MasterVC

 -(void)insertTeamWithName:(NSString *)name andColors:(NSString *)colors { NSManagedObjectContext *contextx = [self.fetchedResultsController managedObjectContext]; Team *newTeam = [NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:context]; [self saveContext]; } 


7) if you edited the command, it will be called the saveContext method in MasterVC

 -(void)saveContext { NSManagedObjectContext *context = [self.fetchedResultsController managedObjectcontext]; NSError *error = nil; if ( ! [context save:&error] ) { NSLog(@"Error"); } } 



Core Data for the iOS Sound System Series

it is considered one of the best books for basic data, even if it is outdated it has a completely different approach.

we have RootVC and PersonEditorVC, which we can add and change Person

1) when the add button for the rootVC button is pressed, it calls the setCurrentPerson: nil method in PersonEditorVC and opens this view

 - (void)addNewPerson { [self.personEditorViewController setCurrentPerson:nil]; [self.navigationController pushViewController:self.personEditorViewController animated:YES]; } 


2) the PersonEditorVC setCurrentPerson method is called by the previous method. if the person is equal to zero, he calls the initializer Person, if the person is not equal to zero, he gets his id object

 - (void)setCurrentPerson:(AWPerson *)aPerson { if( !aPerson ) { self.title = @"Add person"; aPerson = [AWPerson personInManagedObjectContext:self.editingContext]; } else if( [aPerson managedObjectContext] != self.editingContext ) { self.title = @"Edit person"; aPerson = (id)[self.editingContext objectWithID:[aPerson objectID]]; } if( currentPerson != aPerson ) { [currentPerson release]; currentPerson = [aPerson retain]; } } 


3) PersonEditorVC opens n sets its own text field, if the person is not equal to zero, then gets the edited value from the text field for each text FieldShouldReturn

 - (void)textFieldDidEndEditing:(UITextField *)textField { if( textField == self.firstNameTextField ) [self.currentPerson setFirstName:textField.text]; else if( textField == self.lastNameTextField ) [self.currentPerson setLastName:textField.text]; [self becomeFirstResponder]; } 


4) when the user clicks cancel, he simply returns to the old controller, if he clicks the "Save" button, he simply saves the context

 - (IBAction)savePerson:(id)sender { NSError *anyError = nil; BOOL success = [[currentPerson managedObjectContext] save:&anyError]; if( !success ) { UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Couldn't save this person" message:[anyError localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [errorAlert show]; } else { [self.navigationController popViewControllerAnimated:YES]; } } 


5) it is important to note that the initializer wrote it in the auto-generated model class

 + (id)personInManagedObjectContext:(NSManagedObjectContext *)moc { return [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:moc]; } 


1 edit), if the user wants to edit the person and click on the person’s row in the table, he receives a call to the tableview method, where he simply calls setCurrentPerson, with the person

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { AWPerson *selectedPerson = [self.fetchedResultsController objectAtIndexPath:indexPath]; [self.personEditorViewController setCurrentPerson:selectedPerson]; [self.navigationController pushViewController:self.personEditorViewController animated:YES]; } 



conclusions

here we have 3 completely different approaches, from the 3 best books on Core Data.

What is your approach? are you using another? why do you like more than the other?

I personally think that the last one is the best, even if it may be less easy to code, it is certainly better syntax, more reusable, etc. but for a small application, I would probably use the first one.

+4
source share

Source: https://habr.com/ru/post/1500978/


All Articles