I retrieve Realm objects named "DictObj" from the list of "index" properties as follows:
NSArray *listIDs = @[@1000,@0,@100,@4];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"index IN %@",listIDs];
self.fetchedResults = [DictObj objectsInRealm:realm withPredicate:predicate];
I just retrieve the results, but automatically order the primary key "index", I want to save it in the order of "listID". Therefore, I create a sortDescriptor and try to configure it as below, but it crashes with an error: "Application termination due to unselected exception" NSInvalidArgumentException ", reason: '- [NSSortDescriptor property]: unrecognized selector sent to instance 0x"
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"index" ascending:NO comparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSNumber *num1 = [NSNumber numberWithInteger:(NSInteger)obj1];
NSNumber *num2 = [NSNumber numberWithInteger:(NSInteger)obj2];
NSInteger listIndex1 = [listIDs indexOfObject:num1];
NSInteger listIndex2 = [listIDs indexOfObject:num2];
return listIndex1 < listIndex2;
}];
self.fetchedResults = [[DictObj objectsInRealm:realm withPredicate:predicate] sortedResultsUsingDescriptors:@[sortDescriptor]];
@interface DictObj : RLMObject
@property NSInteger index;
@property NSString *name;
@end
How to set up sorting of result order by list in Realm?
source
share