Basic data: I want pk in my selected objects

I want to have pk in my imported objects, so I can use a unique pk number for a unique image file name.

But I can’t make it work, I just need a unique file name for my images. Does anyone have a solution for this?

When I get an NSLog object ID, I get the following:

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

Output: ID: 0x124dd00

I know the last p11 is like pk, but what is the best way to get to it?

+4
source share
4 answers

Instead of using an object identifier that is not constant during the life cycle of an object, we can create a unique UUID and save it as an attribute. UUID can be generated as follows:

In a subclass of your managed entity, 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 your attribute for storing the UUID is called uuid.

+10
source

sorry for the late reply:)

You could simply do (assuming that, as others have said, object identifiers are already permanent for your object):

[[[myManagedObject objectID] URIRview] lastPathComponent]

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

Hope this helps Ken

+2
source

You do not get direct access to SQL PK, as this is an internal part used by the CD for unique objects and is subject to change. You can use NSManagedObjectID , though:

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

You can then use this URL as the file name. You probably want to remove the scheme and some other bits, but this is not necessary if you encode the URL so that it only has characters that are legalized in file names. Just make sure that the object has already been saved when you do this, or that the identifier will not be a persistent identifier (see the NSManagedObjectID documentation for more information).

0
source

OKe thanks, now I know that I am looking in the right direction! When I NSLog all my objects look the same except for 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 file name. Therefore, I have my files named like this:

p25 p26 p27 p28

0
source

All Articles