核心数据:保存唯一的对象ID

发布于 2024-12-05 11:59:21 字数 195 浏览 4 评论 0原文

我看到有一种方法可以获取托管对象的唯一 ID:

NSManagedObjectID *moID = [managedObject objectID];

但是,正如我所读到的,它在保存时会发生变化。

有什么更好的方法来创建我自己的唯一 ID 并将其保存在每个对象中,确保它是唯一的并且不会更改?

I see that there is a method of getting the unique id of a managed object:

NSManagedObjectID *moID = [managedObject objectID];

But, as I have read, that changes when it is saved.

What is a better way of creating my own unique ID and saving it in each object, making sure that it IS unique and does't change?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伪心 2024-12-12 11:59:22

您无法将 NSManagedObjectID 保存在 CoreData 实体中,并且其中没有可用作整数或字符串 ID 的属性。
因此,构建您自己的唯一标识符算法是一个可接受的解决方案,如果您无法跟踪保存实体的时间,我对某些应用程序这样做了。

例如,我不久前遇到了类似的问题,当时我需要用对实体的引用填充 UITableView 单元格,并在单击/触摸单元格后检索实体。

我通过使用 uri 表示找到了一个建议,但是但是我仍然需要之前保存上下文,但我设法使用 NSFetchedResultsController 并找到了一个更可靠的解决方案,而不是应用程序构建的 id。

[[myManagedObject objectID] URIRepresentation];

然后您可以稍后检索托管对象 id 本身:

NSManagedObjectID* moid = [storeCoordinator managedObjectIDForURIRepresentation:[[myManagedObject objectID] URIRepresentation]];

并且使用 moid 我可以检索您的托管对象。

You cannot save the NSManagedObjectID in a CoreData entity, and there's no properties in it that can be intended as integer or string ID.
Therefore building your own unique identifier algorithm is an acceptable solution, if it's impossible for you to keep track when the entity is saved, I did this for some applications.

For example I had a similiar problem time ago, when I needed to fill a UITableView cells with a reference to the entity, and retrieve the entity after clicking/touching the cell.

I found a suggestion by using the uri representation, but however I still needed to save the context before, but I manage to use the NSFetchedResultsController and found a more solid solution rather than an application built id.

[[myManagedObject objectID] URIRepresentation];

Then you can later retrieve the managed object id itself:

NSManagedObjectID* moid = [storeCoordinator managedObjectIDForURIRepresentation:[[myManagedObject objectID] URIRepresentation]];

And with the moid I could retrieve your managed object.

放我走吧 2024-12-12 11:59:22

我的对象中有一个创建的日期属性,所以我最终使用了该日期(包括秒),在我看来,它将是唯一的并且可以按照我想要的方式工作。

I have a created date property in my object, so I just ended up using that date including seconds, which, seems to me like it will be unique and work how I want.

何止钟意 2024-12-12 11:59:22

您可以为对象创建一个 id 字段,并在 init 期间使用 GUID 填充它。有关如何创建 GUID 并可选择将其导出为字符串,请参阅 Cocoa 中的 UUID (GUID) 支持

You can create an id field for your object, and populate it during init using GUID. for how to create a GUID and optionally export it to string see UUID (GUID) Support in Cocoa

长伴 2024-12-12 11:59:22

如果它可以帮助其他人搜索这个,我就是这样做的:

在对象上创建一个 ID 属性

使用以下代码创建对象时获取最后使用的 ID:

Playlist *latest;

// Define entity to use and set up fetch request
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Playlist" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];


// Define sorting
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"listID" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];


// Fetch records and handle errors
NSError *error;
NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&error];

if (!fetchResults) {
    NSLog(@"ERROR IN FETCHING");
}

if ([fetchResults count] == 0) {
    latestID = 0;
} else {
    latest = [fetchResults objectAtIndex:0];
    latestID = latest.listID;
}

在新对象上将其增加 1。

If it helps anyone else searching for this, this is how I do it:

Create an ID property on the object

Get the last used ID when creating an object with this code:

Playlist *latest;

// Define entity to use and set up fetch request
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Playlist" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];


// Define sorting
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"listID" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];


// Fetch records and handle errors
NSError *error;
NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&error];

if (!fetchResults) {
    NSLog(@"ERROR IN FETCHING");
}

if ([fetchResults count] == 0) {
    latestID = 0;
} else {
    latest = [fetchResults objectAtIndex:0];
    latestID = latest.listID;
}

Increment this by one on the new object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文