How to sort a real-time result order by list?

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]];

//Model
@interface DictObj : RLMObject
@property NSInteger index;
@property NSString *name;
@end

How to set up sorting of result order by list in Realm?

+4
source share
1

-[RLMResults sortedResultsUsingDescriptors:] NSArray<RLMSortDescriptor *>. NSArray<NSSortDescriptor *>. RLMSortDescriptor NSSortDescriptor - , .

RLMResults . , , NSArray RLMResults, . RLMResults № 1265 Realm GitHub.

+4

Source: https://habr.com/ru/post/1621228/


All Articles