Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core data: I want the pk in my fetched objects

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??

like image 965
Ton Avatar asked Dec 18 '22 06:12

Ton


1 Answers

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.

like image 145
diederikh Avatar answered Dec 28 '22 07:12

diederikh