views:

761

answers:

4

I want to have the pk in my fetched objects, so i can use the unique pk number for a unique image filename.

But i can't make it work, i just need a unique filename for my images. Does somebody has a solution for this?

When i NSLog object ID i get this:

NSManagedObjectID *ID = [someThing objectID]; NSLog(@"ID: %@",ID);

Output: ID: 0x124dd00

I know the last p11, resembles the pk, but whats the best way to get to it??

A: 

You do not get direct access to the SQL PK since that is an internal detail used by CD to unique the objects and could change. You can use the NSManagedObjectID though:

- (NSURL *) uniqueURLForManagedObject:(NSManagedObject *)managedObject {
  NSManagedObjectID *objectID = managedObject.objectID;
  NSURL *objectURL = managedObject.URIRepresentation

  return objectURL;
}

You can then take that URL use it as a file name. You probably want to remove the schema and some other bits, but that isn't necessary if you encode the URL in such a way that it has only characters that legal in file names. Just make sure that the object has already been saved out when you do this or the ID will not be a permanent ID (checkout NSManagedObjectID's documentation for more details).

Louis Gerbarg
Be aware that the objectID will change once the object has been saved to the persistent store! So you can only write your image after the object has been saved.
Diederik Hoogenboom
If you read the answer you would see that last sentence of it is "Just make sure that the object has already been saved out when you do this or the ID will not be a permanent ID (checkout NSManagedObjectID's documentation for more details)."
Louis Gerbarg
On 10.5 and later, you can obtain a permanent ID for an unsaved objects. -[NSManagedObjectContext obtainPermanentIDsForObjects:error:]
Jim Correia
A: 

OKe thanks, now i know i am looking in the right direction!, when i NSLog all my objects they look all the same, except the last bit.

ID1: x-coredata://5B58789C-70CE-40D7-B0BA-B9F56DEBFF61/Things/p28
ID1: x-coredata://5B58789C-70CE-40D7-B0BA-B9F56DEBFF61/Things/p27
ID1: x-coredata://5B58789C-70CE-40D7-B0BA-B9F56DEBFF61/Things/p26
ID1: x-coredata://5B58789C-70CE-40D7-B0BA-B9F56DEBFF61/Things/p25

Can't i just skip the first part and just use the last bit for my filename. So i have my files named like so:

p25 p26 p27 p28

Ton
+5  A: 

Instead of using the objectID which is not constant during the object's life cycle us can generate a unique UUID and store that as an attribute. The UUID can be generated as follows:

In a subclass of your managed object add the following code to your awakeFromInsert:

- (void)awakeFromInsert;
{
    [super awakeFromInsert];
    CFUUIDRef UUID = CFUUIDCreate(kCFAllocatorDefault);
    CFStringRef UUIDString = CFUUIDCreateString(kCFAllocatorDefault,UUID);
    [self setValue:(NSString *)UUIDString forKey:@"uuid"];
    [UUID autorelease];
    [UUIDString autorelease];
}

This assumes that your attribute to store the UUID is called uuid.

Diederik Hoogenboom
A: 

Hi Ton, sorry for the late reply :)

You could just do (assuming as others have said, the objectIDs are already permanent for your object):

[[[myManagedObject objectID] URIRepresentation] lastPathComponent]

... to get the "p28" part etc.

Hope this helps, Ken

The boy Ken