Thursday, 5 September 2013

Core Data - many ways to add an object

Core Data - many ways to add an object

i'm doing some testing of Core Data, let's say i have a mainViewController
with a navigationBar and addButton. Clicking on the addButton will open a
detailViewController. When i press save to insert a new Object the
detailVieController will close and show the table with the new data
inserted. I can think two different way to do that.
FIRST METHOD - Passing the ManagedObjectContext In the action of the add
button i create an instance of the new detailViewController and i pass the
managedObjectContext to it. So will be the save button of the
detailViewController that will take care of saving the context and then
pop the controller.
This is the method called by the addButton in the MainViewController
-(void)addNewObject{
DetailViewController *detVC = [DetailViewController
alloc]initWhit:self.managedObjectCOntext];
[self.navigationcontroller pushViewController:detVC animated:YES];
}
This method is called by the save button in the IngredientViewController
-(void)saveObject{
NSError *error;
if (![self.managedObjectContext save:&error]){
NSLog(@"Error");
}
}
SECOND METHOD - Using a delegate In the action of addButton i create an
instance of DetailViewController, i set it as delegate, so when i press
the save button in the DetailViewCOntroller will call the delegate that
will pass data to the main controller.
This is the method called by the addButton in the MainViewController
(void)addNewObject{
DetailViewController *detVC = [DetailViewController alloc]init];
detVC.delegate = self;
[self.navigationcontroller pushViewController:detVC animated:YES];
}
This method is called by the save button in the IngredientViewController
-(void)saveObject{
[self.delegate detailVCdidSaveObject];
}
This is the delegate implemented in the mainViewController
detailVCdidSaveObject{
NSError *error;
if (![self.managedObjectContext save:&error]){
NSLog(@"Error");
}
}
------------------------------ Passing the object
Is it best to pass raw data to the DetailViewController and create there
the object or it's best to pass the instance of the object to
DetailViewController that will take care of settin its data?
For Example
This way i link the object instance of the mainVC to the one DetailVC so i
can easilly set its value
-(void)addObject{
DetailViewController *detailVC =[[DetailViewController alloc]init];
detailVC.delegate = self;
self.object = [NSEntityDescription
insertNewObjectForEntityForName:@"Object"
inManagedObjectContext:self.managedObjectContext];
detailVC.object = self.object;
[self.navigationController pushViewController:detailVC animated:YES];
}
this way i pass raw data and let the detailVC create the instance
-(void)addObject{
DetailViewController *detailVC =[[DetailViewController
alloc]initWithName:@"objname"];
[self.navigationController pushViewController:detailVC animated:YES];
}
those code are just pseudocode for educational purpose. all ways works, i
just want to know which do you think it's the most correct and why. thanks

No comments:

Post a Comment