small medium large xlarge

Back to: All Forums  Core Data
Generic-user-small
16 Oct 2009, 23:19
Duncan Groenewald (12 posts)

Can anyone explain why Core Data does not maintain referential integrity in the following scenario, or is it that I am doing something wrong…

Basic requirement is to create a list of say projects and for each project to be able to create a hierarchical list of tasks (and subtasks).

So I am using a list box to display the list of projects and then a tree to display the task hierarchy. However it seems that Core Data does not maintain a link between all the tasks and the project. Only the top level tasks contain references to the project. As you can imagine this creates all kinds of downstream problems, for example trying to create a report, or calculating a sum of task attributes by project.

Any suggestions on how to overcome this would be welcome - its a bit of a show stopper…

I’ll send the same code if that will help - let me know via email (man15dg@gmail.com).

Thanks

Avatarsmall_pragsmall
27 Oct 2009, 00:05
Marcus S. Zarra (284 posts)

Core Data does maintain referential integrity if the data model is designed correctly. I would suggest taking a picture of your data model and posting it here so we can see what is going on.

Generic-user-small
23 May 2010, 02:01
Duncan Groenewald (12 posts)

Sorry be busy on other stuff but still would like to find a way to address my problem with Core Data.

Here is the model http://www.facebook.com/album.php?aid=57606&id=1537898582&l=11215761c7.

In this model a Project has Tasks and Tasks can have subtasks. However every subtask also belongs to the Project and I would expect that each Task record has a ProjectCode field populated with the ProjectCode regardless of the level in the hierarchy.

For some reason the model only populates the Tasks ProjectCode if it is a task in the top level of the hierarchy. Subtasks do not get the ProjectCode populated. So unless I make ProjectCode optional trying to save throws an exception.

Not having the ProjectCode populated creates more problems for me when I try and move an Task around in the Hierarchy. Anytime I move a subTask up to the top level in the hierarchy it dissappears - because it does not have the ProjectCode populated.

Thanks

Generic-user-small
17 Aug 2010, 01:22
Duncan Groenewald (12 posts)

Just a quick update…

Marcus - I think I agree with the comments in your book where you recommend avoiding using the NSTreeController although I am not sure its not a broader combination of things that don’t play well together. I found some examples where others have encountered the same problems I have when trying to implement the model using Core Data and Bindings. You do have to write code to populate the ProjectCode (or ID) for subtasks because the standard Core Data and Bindings will not do so. It seems the whole thing breaks down when you use an NSArrayController for Projects and an NSTreeController for the Tasks with the NSTreeController content set by the selection in the Project NSArrayController. Adding new Tasks to a specific point in the TreePath does not work and new Tasks seem to be created in random positions. This appears to be as a result of creating the object in the ManagedObjectContext - as soon as this is done the object appears in the NSOutlineView even before the object is inserted into the TreeController TreePath. And of course this would be because on creation neither the parent property nor the project property would be populated - and top level items have the parent property as nil. So presumably any time you create an object it will for a fleeting moment appear in the top level of the hierarchy - unfortunately mine don’t disappear later when these properties are correctly set.

I guess its time to try using the data source protocol as per your suggestion. Are you able to offer any suggestions or examples of how to do this ?

Avatarsmall_pragsmall
17 Aug 2010, 03:11
Marcus S. Zarra (284 posts)

The data source methods are actually very easy to use. There are only a couple of methods that you need to implement (and I recommend implementing them on whatever the controller for that window is). With the protocol declared in your controller you need to retrieve the relevant core data objects, sort them and hang onto them in an array. Then you can easily answer the methods that the data source requires. Should be fairly straight-forward.

You must be logged in to comment