Core Data fetches based on properties of 'ordered' relationships?

Well, assuming I have understood the problem correctly, I'd do it like this. Lets say you've got two entities, TopEntity has a (NSString *)name property and a to-many relationship to MyEntity which has a (NSString *)data property and (NSInteger)order property.

Well, assuming I have understood the problem correctly, I'd do it like this. Lets say you've got two entities, TopEntity has a (NSString *)name property and a to-many relationship to MyEntity which has a (NSString *)data property and (NSInteger)order property. Lets say you want the TopEntity objects which match a given string, and whose MyEntity orders are satisfy a certain condition, then you can do it with two predicates and an NSFetchRequest like so.... NSManagedObjectContext *context = self managedObjectContext; // Create some top level entities TopEntity *aTop = TopEntity insertInManagedObjectContext:context; aTop.Name = @"This is Your Name"; TopEntity *bTop = TopEntity insertInManagedObjectContext:context; bTop.

Name = @"This aint a Name"; TopEntity *cTop = TopEntity insertInManagedObjectContext:context; cTop. Name = @"This is My Name"; // Add some data NSInteger i, len = 30; for(i=0; iOrderValue = i; entity. Data = NSString stringWithFormat:@"This is some data: %d", i; if(i Name); } So, essentially you can just wrap the results of your fetch request with another predicate and use the ANY keyword.

I've got no idea how efficient that is, but it works for this case. Running the above will output "This is Your Name" URL2 matches the first TopEntity.

I'm not sure why I didn't think about this, but it's even easier. Just use one predicate with a format something like @"(name BEGINSWITH %@) AND (ANY objects. Order > %d)".

The equality on the order property depends on what you want, I'm not sure if you want to limit the result based on the n-recent to-many object's data, or just the n-recent objects. I find the NSPredicate format string docs pretty useful: developer.apple. Com/library/mac/#documentation/cocoa/Conceptual/… – Daniel Thorpe Nov 27 '10 at 0:09 Hmm, that last solution of using the order index is actually pretty good.

Could certainly work for a few cases I have. Thanks! The original solution was also good, but wouldn't fit very well into my generic 'Smart Folder' approach of searching on a single predicate.

– Drew McCormack Nov 27 '10 at 9:58.

I don't think there's a way to limit to n results in a predicate, only at the fetch request level. Aside from referencing the last n items in a relationship as you mentioned, you might try a boolean attribute "lastN" and flip them on/off when you curate the order of the list (say, during user-initiated sort or drag-and-drop reordering). Alternatively, you could create a separate fetch request for each searched thing that sorts by your sort key, ordered descending, and is limited (via -setFetchLimit: ) to n results.

Tracking this as a relationship or an attribute is somewhat "messy" whereas the fetch limit is more expensive (because of multiple round trips). If your reordering is done by one-off user actions, it might be better performance-wise to use the relationship or attribute approach since the work is amortized rather than done all at once in a series of fetches. I haven't found a better way myself and will follow this one closely.

:-).

I was hoping to be able to do it with a single predicated fetch, but I'm starting to think it can't be done. I get the feeling using extra properties - denormalizing - is probably the best way to go, performance-wise too. – Drew McCormack Nov 26 '10 at 21:34 Certainly the one-fetch-request-per-object-searched approach is atrociously wasteful and unreasonable for a large list of searched objects, but for small lists it can be a reasonable hit.

I'd personally use the "lastN" flags approach. I think (but don't quote me) relationships automatically mean multiple round-trips with Core Data. – Joshua Nozzi Nov 26 '10 at 21:44.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions