Creating a new object in Core Data: create the first or after return?

So, I am struggling with Core Data. I find that there are many ways to do the same, and that if you try to create an application using storyboards and UIManagedDocuments, you need to read all the tutorials and examples that were older than last year with a translation sheet. The question today is finding best practice when adding a new managed facility. I saw examples in both directions:

  • Create a new managed object in the table view controller (after pressing +) and give this new radiant managed object a subordinate controller of the "Add" type to get user input for all the attributes of the object. It seems simple, and the returned object is easy to understand, since it contains all the individual attributes. But I saw an example of code in this view controller "Add" for the "Cancel" button, which deletes the transferred managed object and then calls "Save context" before rejecting it. A functional but training gnome on MVC on my shoulder yells at me that this Add View slave will delete the object, and the horrors will directly call the Save Context. Apple's recipe sample code seems to use this method.

  • Send nothing to the add view controller and send it a delegate call using the table view controller, which returns each of the attributes as a separate passed parameter. Thus, the return method becomes very long: controller: (UIViewController *) controller didReturnFirstName: (NSString *) firstName and LastName: (NSString *) lastName and StreetAddress: (NSString *) and ... and ... and .. But this is SO according to the MVC dogma, because the managed entity is created back in the table view controller when it receives all the individual attributes, and the Add view never touches the model (main data) or throws out an unused managed entity when the user changes their minds.
    Even with chained delegated methods, I still argue with myself, which is the best method. Comments and ideas from those who lived with both forms would be a welcome addition.

Thank.

+3
source share
4 answers

You look at an example in the Apple tutorial , they perform this task by performing a number of things, described below, in which case they have a modal representation to enter information that should be added to the data model:

  • In the modal representation that appears, they create a protocol for processing either rejecting the representation or storing data, and the properties of the type identifier that implements this protocol as delagate, this ensures that any object requires the methods

  • , , ,

  • , , ,

, , : .h

 @protocol yourProtocol <NSObject>;
 //methods that determine what happens based on what user does, it would save your core data object
 @end

 @property(nonatomic, weak) id<yourProtocol> delegate;

modal view.m , , , , , IBAction,

 [self.delegate myMethod];

, , .h

 @interface viewController() <yourProtocol>

, , , modal view.m, . Apple , , / .., , . , , , .

+1

, .

, , , , , :

  • NSManagedObject, , "".
  • , ( ). .
  • "" .
    . "", , , .
    . "", , ( ) , "" ".
+1

, NSManagedObjectContext AddController ( ) EditController ( ).

A) B) SDK:

A) , .

"" TableController ManagedObjectContext (NSManagedObjectDidSaveNotification).

"" .

B) , OSX 10.7 (+), NSManagedObjectContext s, NSManagedObjectContext AddController ( ) parentContext TableController NSManagedObjectContext.

"" .

"" .

. Apple CoreDataBooks http://developer.apple.com/library/ios/#samplecode/CoreDataBooks/Introduction/Intro.html

0

, , () , , , . ,

  • ,
  • , , .

Nicks Apple () , , , , , , -.

, : http://robsprogramknowledge.blogspot.pt/2012/05/back-segues.html

Rob , , / (Nick's), ( " " ) - , - - . , , ,

" " , , , - , (), , .

- , , , 1. 2. 3.

Inafzigers - , ? ?

Apple 'CoreDataBooks, , , ManagedObjectContext (insertNewObjectForEntityForName) prepareForSegue ( ): - "", ManagedObjectContext, FetchedResultsContext - "", , , , ,

, , , :

  • (CoreDataBooks RootViewController.m), , , , . :  : . , - - , . , (, ).

  • CoreDataBooks UIManagedDocument - ,

, insertNewObjectForEntityForName ? , , ?

Also, if we do Edit and Cancel, can we use the undo manager to get rid of the changes?

0
source

All Articles