开发者

How do I order core data fetched results from one datastore based on data in another datastore

I'm building an iPhone application where I have two datastores, one for static data and one for dynamic data. I use two separate core data stacks for this. The dynamic data includes an entity and attribute for ID and another attribute for a date. The ID attribute references a matching ID in an entity in the static datastore.

I would like to fetch items from the static store that have ID's in the dynamic datastore and order them based on the dates in the dynamic datastore. I have both googled this, and searched through questions here about cross-store fetched properties, but I have not managed to find a solution to this. I can fetch the correct items from the static store, but not in the correct order, so my ques开发者_JAVA技巧tion is this: Can I somehow (perhaps with fetched properties) fetch the static items sorted by the dates in the dynamic store, or do I need to sort them in code after I fetch them?

EDIT: An entity in the static store might be referenced by more than one entity in the dynamic store.


You can add a transient property to your entity in the managed object model. Transient properties are calculated on demand, so this would be the appropriate place to do the lookup in the other stores.

Then in your fetch request you simply add a sort descriptor which references the transient property on the target object.

In the implementation for the transient property, look up the matching record in the other store and return the date value.

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdNSAttributes.html

It's not clear to me why you have chosen to segregate the data though, and this is an especially fragile paradigm as you will have to either eat the performance hit of maintaining the cross store reference, or risk being unable to locate the record in the dynamic store.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜