I’m trying to use segues for passing core data MOC and Entities to other View Controllers.
So I’m using the prepareForSegue method and doing something like this
SecondViewController *svc = (SecondViewController *)[segue destinationViewController];
//passing the current managed object context to the other view controller
svc.managedObjectContext = managedObjectContext
I then want the pass the currentEntity to the same view controller
//rootEntity is -- TheManagedObject * rootEntity in the second view controller
svc.rootEntity = currentEntity
I’m not sure if the above svc.rootEntity is the right way to do it but it feels like the right way to do it to inject the currentEntity in the next view controller.
In the Second View Controller I want to insert a new object for the entity based on the rootEntity injection above.
I know I would normally create a new Managed Object by doing this:
NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:@"TheNewObject" in ManagedObjectContext:managedObjectContext //MOC injected from the First View Controller
My issue is that I want to do the above newObject but I want it to be dependent (relationship) to the first passed entity (the above rootEntity).
I’ve come close but I keep getting unassociated NewObjects (should be one to many)
The next step would the be to repeat the above and insert another level in the next view controller based on the the NewObject in the second view Controller.
I’ve read Zarra’s book and a few others but they all use init methods that don’t seem to work with segues.
Thanks,
you are doing everything right. Once you are in your new view controller, just proceed as you would originally when inserting new entities and relationships. After all, you are referring to the same managed object context.
So for example, if you want to insert an new entity which is a relationship you would do something like this:
The newObject of kind “SubEntity” is now associated to the
rootObject.