CoreData按属性的唯一对象 [英] CoreData unique objects by attribute

查看:193
本文介绍了CoreData按属性的唯一对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图形与一种对象称为消息。消息可以有childs,所以图形是一棵树。
图上的每个对象都包含一个属性值;这是一种UUID(全局唯一),所以存储不能包含多个消息具有相同的UUID(这个uuid是从消息数据,所以我不能替换它使用nsmanagedobject的objectID)的字符串。
当我需要验证对象插入到存储中时,问题发生。
如何检查具有相同UUID的消息是否已存在于商店中?我正在考虑对validateForInsert:事件的fetch请求,但是它似乎太慢/复杂,当有很多对象要插入(在我的情况下约30k)。
任何人都有最好的解决方案?

I have a graph with one kind of object called Message. A message could have childs so the graph is a tree. Each object on the graph contains an attribute value; that's a sort of UUID (globally unique) so the store cannot contains multiple messages with the same UUID (this uuid is a string taken from message data so I can't replace it using the objectID of nsmanagedobject). The problem happend when I need to validate object insertion into the store. How can I check if a message with the same UUID is already present into the store? I'm thinking about a fetch request on validateForInsert: event but it seems too slow/complex when there are lots of objects to insert (about 30k in my case). Anyone have a best solution?

推荐答案

这是CoreData的一个已知限制对我自己)。您还应该转到 http://bugreport.apple.com ,让他们知道您需要此功能。 (原始错误ID是rdar:// 3711805)

This is a known limitation with CoreData (I've filed a feature request on this myself). You should also go to http://bugreport.apple.com and let them know you want this feature. (The original bug id is rdar://3711805)

过去我已经得到的方法是使用一个方便的方法来访问和创建我的 NSManagedObjects 。这个方便方法然后在一个静态的 NSMutableDictionary 中查找是否已经创建了具有相同唯一属性的另一个对象(唯一属性是键,managedObjectID是值)。如果它找到一个,它只是返回该对象。如果没有,那么它将创建一个并将对象的ID缓存到静态字典中以供将来使用。当应用程序第一次启动时,我必须预先填充这个字典的预先存在的对象的属性/标识符。

The way I've gotten around this in the past is to use a convenience method to access and create my NSManagedObjects. This convenience method then looks up in a static NSMutableDictionary to see if another object with the same unique attribute has already been created (the unique attribute being the key, and the managedObjectID being the value). If it finds one, it just returns that object instead. If it doesn't, then it goes about creating one and caching that object's ID into the static dictionary for future use. When the app first launches, I have to pre-populate this dictionary with the attributes/identifiers of pre-existing objects.

这是一个痛苦,我知道。 :(文件增强请求。

It's a pain, I know. :( File an enhancement request.

这篇关于CoreData按属性的唯一对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆