From 163ae5c864ae48a470d2055eeb2a9c318689fe4e Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Sun, 19 Jun 2022 02:05:26 -0700 Subject: [PATCH] [Core Data] Optimize queue and shuffle loading Optimize queue loading code, and add shuffle list reading code. Signed-off-by: Christopher Snowhill --- Playlist/PlaylistController.h | 4 ++++ Playlist/PlaylistController.m | 41 +++++++++++++++++++++++++++++++++++ Playlist/PlaylistLoader.m | 20 ++--------------- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/Playlist/PlaylistController.h b/Playlist/PlaylistController.h index 1b94ea1ea..9ecb104f4 100644 --- a/Playlist/PlaylistController.h +++ b/Playlist/PlaylistController.h @@ -131,6 +131,10 @@ typedef NS_ENUM(NSInteger, URLOrigin) { - (void)emptyQueueListUnsynced; - (NSMutableArray *_Nullable)queueList; +// internal methods for data store init +- (void)readQueueFromDataStore; +- (void)readShuffleListFromDataStore; + // reload metadata of selection - (IBAction)reloadTags:(id _Nullable)sender; diff --git a/Playlist/PlaylistController.m b/Playlist/PlaylistController.m index 72779949e..170390eb9 100644 --- a/Playlist/PlaylistController.m +++ b/Playlist/PlaylistController.m @@ -1242,6 +1242,47 @@ static void *playlistControllerContext = &playlistControllerContext; return temp; } +- (void)readQueueFromDataStore { + NSPredicate *deletedPredicate = [NSPredicate predicateWithFormat:@"deLeted == NO || deLeted == nil"]; + NSPredicate *queuedPredicate = [NSPredicate predicateWithFormat:@"queued == YES"]; + + NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"queuePosition" ascending:YES]; + + NSCompoundPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[deletedPredicate, queuedPredicate]]; + + 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) { + [queueList removeAllObjects]; + NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [results count])]; + [queueList insertObjects:results atIndexes:indexSet]; + } +} + +- (void)readShuffleListFromDataStore { + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"deLeted == NO || deLeted == nil"]; + + NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"shuffleIndex" 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) { + [shuffleList removeAllObjects]; + NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [results count])]; + [shuffleList insertObjects:results atIndexes:indexSet]; + } +} + - (void)addShuffledListToFront { NSArray *newList; NSIndexSet *indexSet; diff --git a/Playlist/PlaylistLoader.m b/Playlist/PlaylistLoader.m index bda03494b..5f1bb8c79 100644 --- a/Playlist/PlaylistLoader.m +++ b/Playlist/PlaylistLoader.m @@ -827,24 +827,8 @@ static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_bloc [playlistController insertObjectsUnsynced:results atArrangedObjectIndexes:is]; } - [playlistController emptyQueueListUnsynced]; - - NSMutableDictionary *queueList = [[NSMutableDictionary alloc] init]; - - for(PlaylistEntry *pe in results) { - if(pe.queued && pe.queuePosition >= 0) { - NSString *queuePos = [NSString stringWithFormat:@"%llu", pe.queuePosition]; - [queueList setObject:pe forKey:queuePos]; - } - } - - if([queueList count]) { - for(size_t i = 0, j = [queueList count]; i < j; ++i) { - NSString *queuePos = [NSString stringWithFormat:@"%zu", i]; - PlaylistEntry *pe = [queueList objectForKey:queuePos]; - [[playlistController queueList] addObject:pe]; - } - } + [playlistController readQueueFromDataStore]; + [playlistController readShuffleListFromDataStore]; return YES; }