Note that there are some explanatory texts on larger screens.

plurals
  1. POCoreData Relationships in two different ManagedObjectContexts
    primarykey
    data
    text
    <p>Warning: This is my first IPhone Application and I'm new to Objective C, therefore my question might be very basic. </p> <p>Actually I have a conceptual problem:</p> <p>I have a one to many relationship between my <code>NSObject</code>s <code>Project</code> and <code>WorkTime</code> (<code>Project&lt;--&gt;&gt;WorkTime</code>). <code>WorkTime</code> has attributes like <code>startDate</code>, <code>endDate</code>, … and a relationship named <code>project</code> to the <code>Project</code> object. </p> <p>On the <code>UITableViewController</code> subclass that shows my list of <code>WorkTime</code>s, I fetch the CoreData Model with a <code>FetchedResultsController</code>. When I create a new <code>WorkTime</code>, I use a second context and merge it with the "default" once, once the user has clicked on "save" on the drill-down View, like in the CoreDataBooks example from the Apple Documentation. That works well. </p> <p>The problem starts when I set the relationship. Since the projects are in the “default” context, I can’t assign any project object to the project key in WorkTime, since they are in two different contexts.</p> <p>So, should I copy all the projects into the “saving” context? Or should I use just one context for everything?</p> <p>One problem that arises when I use only one context is that the tableview that shows the WorkTimes gets actualized when the user clicks on the addbutton (insertNewObjectForEntityForName). So the list grows right before it presents the new view controller (addWorkTimeDetail) in modalviewcontroller. And if the user clicks cancel in that detail view and decides not to save the WorkTime, I’d have to delete the newly created entity from the context. I don’t think this sounds the way to do it.</p> <p>Suggestions would be very appreciated!</p> <p><strong>UPDATE</strong></p> <p>I'm still having some issues:</p> <p>When the AddWorkTimeViewController is created from the RootViewController, I create a new NSObject WorkTime. When the user clicks "cancel" I delete the newly created WorkTime and return to the RootViewController. The FetchedResultsController actualizes the tableView and I see a short animation that comes because the object has been deleted. That's doesn't look nice.</p> <p>I know it would be better to create the object ONLY if the user clicks "save" and by "cancel" do nothing, but I need that object in order to populate the drilldown views with values. The drilldown views work with a NSManagedObject. I see two possible ways to solve this:</p> <p>1) Have a temporary object TempWorkTime subclassed from NSObject with the same attributes from the WorkTime NSManagedObject. I would create the TempWorkTime in the AddWorkTimeViewController and use it for the drilldown views. I would of course have to subclass them to accept an NSObject instead of a NSManagedObject. I guess type casting wouldn't work. When the user clicks "save", I would create a WorkTime NSManagedObject and pass it the values from the TempWorkTime.</p> <p>2) Is it possible to trick the FetchedResultsController so that it doesn't update itself while the tableView is not visible? In that case, it would not respond to the changes in the context until it's back in view, and so it would not do the delete animation.</p> <p>Any suggestions?</p> <p>SECOND UPDATE - maybe it's useful for people trying to do this same thing</p> <p>I found this link which proved to be very helpful:</p> <p><a href="http://www.timisted.net/blog/archive/multiple-managed-object-contexts-with-core-data/" rel="nofollow">http://www.timisted.net/blog/archive/multiple-managed-object-contexts-with-core-data/</a></p> <p>It's very well explained why it's sometimes a good idea to use two contexts. I had some problems with using one context explained in the UPDATE part of my question. </p> <p>Now I'm using two contexts and it works perfectly, even with relationships. Important is to use the NSManagedObjects of the second context when creating the relationship. That was my mistake when I started to work with this approach.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload