From e9b02d54f42993f53f740dfa2b412cdca73221af Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Fri, 17 Jun 2022 17:20:45 -0700 Subject: [PATCH] [Core Storage] Apply index sort to fetch request Apply the index sort to the fetch request itself, rather than applying it to the resulting array afterward. Signed-off-by: Christopher Snowhill --- Playlist/PlaylistController.m | 8 +++----- Playlist/PlaylistLoader.m | 7 ++++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Playlist/PlaylistController.m b/Playlist/PlaylistController.m index 55379b132..26375e1cc 100644 --- a/Playlist/PlaylistController.m +++ b/Playlist/PlaylistController.m @@ -1118,17 +1118,15 @@ static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_bloc NSCompoundPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[deletedPredicate, searchPredicate]]; + NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES]; + NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"PlaylistEntry"]; request.predicate = predicate; + request.sortDescriptors = @[sortDescriptor]; NSError *error = nil; NSArray *results = [self.persistentContainer.viewContext executeFetchRequest:request error:&error]; - if(results && [results count] > 0) { - NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES]; - results = [results sortedArrayUsingDescriptors:@[sortDescriptor]]; - } - return results; } diff --git a/Playlist/PlaylistLoader.m b/Playlist/PlaylistLoader.m index ff8727929..f907824f7 100644 --- a/Playlist/PlaylistLoader.m +++ b/Playlist/PlaylistLoader.m @@ -786,6 +786,10 @@ static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_bloc request = [NSFetchRequest fetchRequestWithEntityName:@"PlaylistEntry"]; + NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES]; + + request.sortDescriptors = @[sortDescriptor]; + results = [moc executeFetchRequest:request error:&error]; if(!results) { ALog(@"Error fetching PlaylistEntry objects: %@\n%@", [error localizedDescription], [error userInfo]); @@ -809,9 +813,6 @@ static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_bloc [playlistController commitPersistentStore]; } - NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES]; - results = [resultsCopy sortedArrayUsingDescriptors:@[sortDescriptor]]; - { NSIndexSet *is = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [results count])];