简体   繁体   中英

NSFetchResultController i am using predicate with attribute in array?

In NSFetchRsultController I am using:

[NSPredicate predicateWithFormat:@"type in %@", myArray];

I want the data in the order of the data in the array, but NSSortDescriptor is mandatory for the NSFetchResultController , so what NSSortDescriptor I have to use?

You can use sortDescriptors to order the data in order of your array.

The sort descriptors specify how the objects returned when the fetch request is issued should be ordered—for example by last name then by first name. The sort descriptors are applied in the order in which they appear in the sortDescriptors array (serially in lowest-array-index-first order).

Code example:

...
NSMutableArray * sortDescriptors = [[NSMutableArray alloc] initWithCapacity:[myArray count]];
for (NSString * key in myArray)
  [sortDescriptors addObject:[[NSSortDescriptor alloc] initWithKey:key ascending:NO]];

[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
...

user1800529,

It appears you want the order sequential order things were inserted into your database. Core Data makes no guarantees about that order. Hence, you need to pick something to sort on. I use creation time, which is in most of my entities, but you could easily use another attribute.

Andrew

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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