@@ -32,59 +32,53 @@ class LibraryStorage: PlayableFileCachable {
32
32
self . context = context
33
33
}
34
34
35
- func resolveDuplicates( ) {
36
- var isDuplicateFound = false
37
-
38
- let allGenreDuplicates = findDuplicates ( for: Genre . typeName)
39
- allGenreDuplicates. forEach {
35
+ func resolveGenresDuplicates( duplicates: [ LibraryDuplicateInfo ] ) {
36
+ duplicates. forEach {
40
37
var genreDuplicates = getGenres ( id: $0. id)
41
38
if genreDuplicates. count > 1 {
42
- isDuplicateFound = true
43
39
let leadGenre = genreDuplicates. removeFirst ( )
44
40
os_log ( " Duplicated Genre (count %i) (id: %s): %s " , log: log, type: . info, $0. count, $0. id, leadGenre. name)
45
41
for genre in genreDuplicates {
46
42
genre. managedObject. passOwnership ( to: leadGenre. managedObject)
47
43
context. delete ( genre. managedObject)
48
44
}
49
45
}
50
- saveContext ( )
51
46
}
52
-
53
- let allArtistDuplicates = findDuplicates ( for : Artist . typeName )
54
- allArtistDuplicates . forEach {
55
- isDuplicateFound = true
47
+ }
48
+
49
+ func resolveArtistsDuplicates ( duplicates : [ LibraryDuplicateInfo ] ) {
50
+ duplicates . forEach {
56
51
var artistDuplicates = getArtists ( id: $0. id)
57
52
let leadArtist = artistDuplicates. removeFirst ( )
58
53
os_log ( " Duplicated Artist (count %i) (id: %s): %s " , log: log, type: . info, $0. count, $0. id, leadArtist. name)
59
54
for artist in artistDuplicates {
60
55
artist. managedObject. passOwnership ( to: leadArtist. managedObject)
61
56
context. delete ( artist. managedObject)
62
57
}
63
- saveContext ( )
64
58
}
65
-
66
- let allAlbumDuplicates = findDuplicates ( for: Album . typeName)
67
- allAlbumDuplicates. forEach {
68
- isDuplicateFound = true
59
+ }
60
+
61
+
62
+ func resolveAlbumsDuplicates( duplicates: [ LibraryDuplicateInfo ] ) {
63
+ duplicates. forEach {
69
64
var albumDuplicates = getAlbums ( id: $0. id)
70
65
let leadAlbum = albumDuplicates. removeFirst ( )
71
66
os_log ( " Duplicated Album (count %i) (id: %s): %s " , log: log, type: . info, $0. count, $0. id, leadAlbum. name)
72
67
for album in albumDuplicates {
73
68
album. managedObject. passOwnership ( to: leadAlbum. managedObject)
74
69
context. delete ( album. managedObject)
75
70
}
76
- saveContext ( )
77
71
}
78
-
79
- let allSongDuplicates = findDuplicates ( for : Song . typeName )
80
- allSongDuplicates . forEach {
81
- isDuplicateFound = true
72
+ }
73
+
74
+ func resolveSongsDuplicates ( duplicates : [ LibraryDuplicateInfo ] ) {
75
+ duplicates . forEach {
82
76
var songDuplicates = getSongs ( id: $0. id)
83
77
let leadSong = songDuplicates. removeFirst ( )
84
78
os_log ( " Duplicated Song (count %i) (id: %s): %s " , log: log, type: . info, $0. count, $0. id, leadSong. displayString)
85
79
for song in songDuplicates {
86
80
song. managedObject. passOwnership ( to: leadSong. managedObject)
87
- deleteCache ( ofPlayable: leadSong )
81
+ deleteCache ( ofPlayable: song )
88
82
if let embeddedArtwork = song. managedObject. embeddedArtwork {
89
83
context. delete ( embeddedArtwork)
90
84
}
@@ -93,12 +87,11 @@ class LibraryStorage: PlayableFileCachable {
93
87
}
94
88
context. delete ( song. managedObject)
95
89
}
96
- saveContext ( )
97
90
}
98
-
99
- let allPodcastEpisodesDuplicates = findDuplicates ( for : PodcastEpisode . typeName )
100
- allPodcastEpisodesDuplicates . forEach {
101
- isDuplicateFound = true
91
+ }
92
+
93
+ func resolvePodcastEpisodesDuplicates ( duplicates : [ LibraryDuplicateInfo ] ) {
94
+ duplicates . forEach {
102
95
var podcastEpisodesDuplicates = getPodcastEpisodes ( id: $0. id)
103
96
let leadPodcastEpisodes = podcastEpisodesDuplicates. removeFirst ( )
104
97
os_log ( " Duplicated Podcast Episode (count %i) (id: %s): %s " , log: log, type: . info, $0. count, $0. id, leadPodcastEpisodes. displayString)
@@ -113,39 +106,30 @@ class LibraryStorage: PlayableFileCachable {
113
106
}
114
107
context. delete ( podcastEpisode. managedObject)
115
108
}
116
- saveContext ( )
117
109
}
118
-
119
- let allPodcastDuplicates = findDuplicates ( for : Podcast . typeName )
120
- allPodcastDuplicates . forEach {
121
- isDuplicateFound = true
110
+ }
111
+
112
+ func resolvePodcastsDuplicates ( duplicates : [ LibraryDuplicateInfo ] ) {
113
+ duplicates . forEach {
122
114
var podcastDuplicates = getPodcasts ( id: $0. id)
123
115
let leadPodcast = podcastDuplicates. removeFirst ( )
124
116
os_log ( " Duplicated Podcast (count %i) (id: %s): %s " , log: log, type: . info, $0. count, $0. id, leadPodcast. name)
125
117
for podcast in podcastDuplicates {
126
118
podcast. managedObject. passOwnership ( to: leadPodcast. managedObject)
127
119
context. delete ( podcast. managedObject)
128
120
}
129
- saveContext ( )
130
121
}
131
-
132
- let allPlaylistDuplicates = findDuplicates ( for : Playlist . typeName ) . filter { $0 . id != " " }
133
- allPlaylistDuplicates . forEach {
134
- isDuplicateFound = true
122
+ }
123
+
124
+ func resolvePlaylistsDuplicates ( duplicates : [ LibraryDuplicateInfo ] ) {
125
+ duplicates . forEach {
135
126
var playlistDuplicates = getPlaylists ( id: $0. id)
136
127
let leadPlaylist = playlistDuplicates. removeFirst ( )
137
128
os_log ( " Duplicated Playlist (count %i) (id: %s): %s " , log: log, type: . info, $0. count, $0. id, leadPlaylist. name)
138
129
for playlist in playlistDuplicates {
139
130
playlist. managedObject. passOwnership ( to: leadPlaylist. managedObject)
140
131
deletePlaylist ( playlist)
141
132
}
142
- saveContext ( )
143
- }
144
-
145
- if isDuplicateFound {
146
- os_log ( " Duplicate Scan: Some duplicates have been found " , log: log, type: . info)
147
- } else {
148
- os_log ( " Duplicate Scan: No duplicates have been found " , log: log, type: . info)
149
133
}
150
134
}
151
135
0 commit comments