核心数据:我想在我的获取对象中使用pk

时间:2022-09-25 12:40:48

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

我想在我的获取对象中包含pk,因此我可以使用唯一的pk编号来获取唯一的图像文件名。

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:

当我NSLog对象ID我得到这个:

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

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

Output: ID: 0x124dd00

输出:ID:0x124dd00

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

我知道最后一个p11,类似于PK,但最好的方法是什么?

4 个解决方案

#1


10  

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:

我们可以生成一个唯一的UUID并将其存储为属性,而不是使用在对象生命周期中不常量的objectID。可以按如下方式生成UUID:

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

在托管对象的子类中,将以下代码添加到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.

这假设您存储UUID的属性称为uuid。

#2


2  

sorry for the late reply :)

这么晚才回复很抱歉 :)

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

你可以这样做(假设正如其他人所说,对象ID已经是你对象的永久物):

[[[myManagedObject objectID] URIRepresentation] lastPathComponent]

[[[myManagedObject objectID] URIRepresentation] lastPathComponent]

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

...获得“p28”部分等

Hope this helps, Ken

肯,这有帮助,肯

#3


0  

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:

您无法直接访问SQL PK,因为这是CD用于使对象唯一且可能更改的内部详细信息。您可以使用NSManagedObjectID:

- (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).

然后,您可以将该URL用作文件名。您可能希望删除架构和其他一些位,但如果您对URL进行编码,使其只包含文件名中合法的字符,则无需这样做。只需确保在执行此操作时该对象已被保存,或者该ID不是永久ID(有关详细信息,请查看NSManagedObjectID的文档)。

#4


0  

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.

好的,谢谢,现在我知道我正朝着正确的方向前进!当我NSLog我所有的物体时,它们看起来都是一样的,除了最后一点。

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

p25 p26 p27 p28

#1


10  

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:

我们可以生成一个唯一的UUID并将其存储为属性,而不是使用在对象生命周期中不常量的objectID。可以按如下方式生成UUID:

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

在托管对象的子类中,将以下代码添加到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.

这假设您存储UUID的属性称为uuid。

#2


2  

sorry for the late reply :)

这么晚才回复很抱歉 :)

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

你可以这样做(假设正如其他人所说,对象ID已经是你对象的永久物):

[[[myManagedObject objectID] URIRepresentation] lastPathComponent]

[[[myManagedObject objectID] URIRepresentation] lastPathComponent]

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

...获得“p28”部分等

Hope this helps, Ken

肯,这有帮助,肯

#3


0  

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:

您无法直接访问SQL PK,因为这是CD用于使对象唯一且可能更改的内部详细信息。您可以使用NSManagedObjectID:

- (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).

然后,您可以将该URL用作文件名。您可能希望删除架构和其他一些位,但如果您对URL进行编码,使其只包含文件名中合法的字符,则无需这样做。只需确保在执行此操作时该对象已被保存,或者该ID不是永久ID(有关详细信息,请查看NSManagedObjectID的文档)。

#4


0  

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.

好的,谢谢,现在我知道我正朝着正确的方向前进!当我NSLog我所有的物体时,它们看起来都是一样的,除了最后一点。

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

p25 p26 p27 p28