Re: Finding managed objects by URI representation
Re: Finding managed objects by URI representation
- Subject: Re: Finding managed objects by URI representation
- From: Gideon King <email@hidden>
- Date: Tue, 6 Apr 2010 01:18:20 +1000
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 - the object ID does get changed to a completely new one (different pointer) upon save, and also I note in the documentation, it warns about the file modification date, so in order to make sure this works, I believe I will have to do the following before I put it into my dictionary (I don't want to have my object directly as an instance variable of the managed object, since the object is at the UI layer, and the managed object layer shouldn't need to know about it):
if ([[myManagedObject objectID] isTemporaryID]) {
NSError *error = nil;
if (![[myManagedObject managedObjectContext] obtainPermanentIDsForObjects:[NSArray arrayWithObject:myManagedObject] error:&error]) {
log4Error(@"Could not obtain permanent ID for %@", myManagedObject);
if (error) {
[NSApp presentError:error];
}
return nil;
}
NSPersistentDocument *document = [[[[self view] window] windowController] document];
[document setFileModificationDate:[NSDate date]];
}
...(create my object)...
[myDictionary setObject:anObject forKey:[myManagedObject objectID]];
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".
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).".
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.
If this is the case, sure, I can create one, but I'm not sure what URL to give it seeing as the user hasn't saved it yet, and besides, I'm not sure if it will have any impact on the behavior when the user actually does save it.
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.
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?
Thanks
Gideon_______________________________________________
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