简体   繁体   English

NSManagedObject临时使用,如何在NSObject和NSManagedObject之间切换

[英]NSManagedObject for temporary use, how to switch between NSObject and NSManagedObject

Im using a Core Data model for my iPhone app. 我为我的iPhone应用程序使用了Core Data模型。 I have been looking for a way to instantiate or use an Entity outside the ManagedObjectContext. 我一直在寻找一种实例化或使用ManagedObjectContext之外的Entity的方法。 (This should not be done, I know, Im also more looking for a way to not do that, but get the benefits anyway). (我知道这是不应该做的,我也正在寻找一种不这样做的方法,但是无论如何要获得好处)。

My challenge is that I have a view where the user can search for "Persons", all the search results are parsed and put into a Person managedObject then displayed in a list. 我面临的挑战是,我有一个视图,用户可以在其中搜索“ Persons”,所有搜索结果都经过解析并放入“ Person ManagedObject”中,然后显示在列表中。 If the user clicks a Person from the list, then and only then would I like the Person entity to be persisted to the store, however this requires me to delete all the other results so they don't get persisted along with the desired one. 如果用户从列表中单击一个Person,则只有在那时,我才希望将该Person实体持久存储到商店中,但是这需要我删除所有其他结果,以使它们不会与所需的结果持久在一起。 Also to the best of my knowledge, if the user decides to quite the app, the store is persisted, potentially with all current search results mixed in with real user data! 据我所知,如果用户决定使用该应用程序,则商店将继续存在,可能会将所有当前搜索结果与真实用户数据混合在一起!

Is there some way I could have a TempPerson NSObject I could use for the search list? 有什么办法可以让我将TempPerson NSObject用于搜索列表? Without, however, me having to manually pull the 45 attributes from the temp object and manually set them on the managedObject! 但是,无需手动从temp对象中提取45个属性并在managedObject上手动设置它们!

Sort of like: NSManagedObject aPersonCorrectlyReturnedFromTheStore = (NSManagedObject *)tempPersonOfJustTypeNSObject 有点像:NSManagedObject aPersonCorrectlyReturnedFromTheStore =(NSManagedObject *)tempPersonOfJustTypeNSObject

I have seen example code from Apple where they build a temporary store to facilitate undo/redo and other stuff on an object that is not yet persisted. 我从苹果公司那里看到了示例代码,他们在其中建立了一个临时存储区,以方便在尚未持久化的对象上进行撤消/重做和其他操作。 This I feel would be overkill in my situation. 我觉得这对我来说太过分了。 I just need to display search results until the user selects a Person to persist. 我只需要显示搜索结果,直到用户选择要保留的人员为止。

Hope it is clear what Im trying to do, feeling like my Core Data vocabulary isn't quite large enough yet:) 希望我清楚我想做什么,感觉我的核心数据词汇还不够大:)

Thanks for any suggestions. 感谢您的任何建议。

You could create each temporary person object as an NSDictionary or NSMutableDictionary. 您可以将每个临时人员对象创建为NSDictionary或NSMutableDictionary。 You can then create a new Person managed object and use the fact that NSManagedObject instances are KVC compliant and use setValuesForKeysWithDictionary:. 然后,您可以创建一个新的Person管理对象,并使用NSManagedObject实例符合KVC的事实,并使用setValuesForKeysWithDictionary:。

New managed objects that are inserted are not actually persisted until you send the managed object context a save: message. 在您向托管对象上下文发送save:消息之前,插入的新托管对象实际上不会持久保存
Keep track of them in a collection (set or array) -- you are probably already doing this since you are presenting the search results somehow. 在集合(集合或数组)中跟踪它们-您可能已经在执行此操作,因为您以某种方式显示了搜索结果。 Then, delete ( deleteObject: ) them all except for the one(s) that the user selects. 然后,删除( deleteObject:)所有这些内容,但用户选择的除外。
The deleted managed objects will never be stored. 删除的管理对象将永远不会被存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM