Re: Finding managed objects by URI representation
Re: Finding managed objects by URI representation
- Subject: Re: Finding managed objects by URI representation
- From: Ben Trumbull <email@hidden>
- Date: Mon, 5 Apr 2010 14:13:50 -0700
On Apr 5, 2010, at 8:18 AM, Gideon King wrote:
> On 05/04/2010, at 6:51 AM, Ben Trumbull wrote:
>
>> No, this is going the wrong way. The objectID is the object's identity in the persistent store (e.g. primary key). You don't need to store pieces of it somewhere else.
>>
>> NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self == %@", myobjectid];
>> or
>> NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self in %@", objectidarray];
>>
>
> Can I just clarify a couple of things about this:
>
> 1. The documentation says "Important: If the receiver has not yet been saved, the object ID is a temporary value that will change when the object is saved." Now I want to use the IDs as keys in a dictionary, which of course copies the keys, and it is very likely that the objects will be added to the dictionary before saving, and referenced after save. I have checked, and the documentation is true
Yes, the documentation is true :-)
> But when I try that, it throws an exception saying "Can't resolve how to assign objects to stores; Coordinator does not have any stores".
What is going on is that you haven't yet saved the NSDocument so there is no database backing it. Until the first save, the document is entirely in memory. We cannot assign a permanent objectID until you actually have a persistent store file.
> The documentation says "Any object not already assigned to a store is assigned based on the same rules Core Data uses for assignment during a save operation (first writable store supporting the entity, and appropriate for the instance and its related items).".
Yes. You can explicitly set the object to a store as well.
> My store class is registered in applicationWillFinishLaunching:, and everything seems to work OK while saving, so I would have expected it to be able to create one if it needed to, but the error seems to imply that I have to manually create my atomic store and add it to the coordinator before this, in order to be able to get it to generate the permanent ID.
That's the store Class. The exception message is that your NSDocument's PSC doesn't actually have any stores (files) added to it at all.
> I just want to check that I haven't gone off on the wrong path again with this, and that I really do have to do all this to get a stable ID...it just seems to me that if I have to do all this just to get a stable ID, maybe there is some more fundamental design issue with my program, or concept I am not getting, and maybe I am trying to work against the way the framework is supposed to be used.
Well, what are you doing such that you need this dictionary that maps object IDs before the document has ever been saved ? Where are you sending this dictionary off to ?
> 2. From the Predicate Programming Guide, it says that SELF refers to the objects that are being searched. I read through the whole guide and found nothing there stating that it could also refer to the managed object ID. I did eventually find it in the Core Data Programming Guide, but even when I knew what I was looking for, it took ages to find. I really think it should be mentioned in the Predicate Programming Guide - should I file a bug against that documentation?
Please do.
- Ben
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden