From e3209e8901a30e28a2e71fc41d60d3d9c57596e4 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Thu, 20 Feb 2025 01:25:44 -0800 Subject: [PATCH] Bug Fix: Play Count data may be missing tags Sometimes the play count data only includes the filenames, and thus will fail a query for just the tags. Also, a file query may be stored without the subsong fragment tag, which will also break the tags. Signed-off-by: Christopher Snowhill --- Playlist/PlaylistEntry.m | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Playlist/PlaylistEntry.m b/Playlist/PlaylistEntry.m index 52b839ac7..9a8042f90 100644 --- a/Playlist/PlaylistEntry.m +++ b/Playlist/PlaylistEntry.m @@ -588,6 +588,8 @@ NSURL *_Nullable urlForPath(NSString *_Nullable path) { NSCompoundPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[albumPredicate, artistPredicate, titlePredicate]]; + __block BOOL fixtags = NO; + __block PlayCount *item = nil; [kPersistentContainer.viewContext performBlockAndWait:^{ @@ -604,6 +606,18 @@ NSURL *_Nullable urlForPath(NSString *_Nullable path) { request.predicate = filenamePredicate; results = [kPersistentContainer.viewContext executeFetchRequest:request error:&error]; + if(!results || [results count] < 1) { + filenamePredicate = [NSPredicate predicateWithFormat:@"filename == %@", self.filename]; + + request = [NSFetchRequest fetchRequestWithEntityName:@"PlayCount"]; + request.predicate = filenamePredicate; + + results = [kPersistentContainer.viewContext executeFetchRequest:request error:&error]; + } + + if(results && [results count] >= 1) { + fixtags = YES; + } } if(!results || [results count] < 1) return; @@ -611,6 +625,19 @@ NSURL *_Nullable urlForPath(NSString *_Nullable path) { item = results[0]; }]; + if(fixtags) { + // shoot, something inserted the play counts without the tags + [kPersistentContainer.viewContext performBlockAndWait:^{ + item.album = self.album; + item.artist = self.artist; + item.title = self.title; + item.filename = self.filenameFragment; + }]; + + NSError *error = nil; + [kPersistentContainer.viewContext save:&error]; + } + return item; }