[Core Data] Optimize queue and shuffle loading

Optimize queue loading code, and add shuffle list reading code.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
swiftingly
Christopher Snowhill 2022-06-19 02:05:26 -07:00
parent 8962403e2a
commit 163ae5c864
3 changed files with 47 additions and 18 deletions

View File

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

View File

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

View File

@ -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;
}