Fix thread-safety with transaction on local media progress

This commit is contained in:
ronaldheft 2022-08-24 19:33:10 -04:00
parent 46623d70a3
commit b5e33b1707
3 changed files with 19 additions and 16 deletions

View file

@ -158,19 +158,24 @@ extension LocalMediaProgress {
}
static func fetchOrCreateLocalMediaProgress(localMediaProgressId: String?, localLibraryItemId: String?, localEpisodeId: String?) -> LocalMediaProgress? {
if let localMediaProgressId = localMediaProgressId {
// Check if it existing in the database, if not, we need to create it
if let progress = Database.shared.getLocalMediaProgress(localMediaProgressId: localMediaProgressId) {
return progress
let realm = try! Realm()
return try! realm.write { () -> LocalMediaProgress? in
if let localMediaProgressId = localMediaProgressId {
// Check if it existing in the database, if not, we need to create it
if let progress = Database.shared.getLocalMediaProgress(localMediaProgressId: localMediaProgressId) {
return progress
}
}
if let localLibraryItemId = localLibraryItemId {
guard let localLibraryItem = Database.shared.getLocalLibraryItem(localLibraryItemId: localLibraryItemId) else { return nil }
let episode = localLibraryItem.getPodcastEpisode(episodeId: localEpisodeId)
let progress = LocalMediaProgress(localLibraryItem: localLibraryItem, episode: episode)
realm.add(progress)
return progress
} else {
return nil
}
}
if let localLibraryItemId = localLibraryItemId {
guard let localLibraryItem = Database.shared.getLocalLibraryItem(localLibraryItemId: localLibraryItemId) else { return nil }
let episode = localLibraryItem.getPodcastEpisode(episodeId: localEpisodeId)
return LocalMediaProgress(localLibraryItem: localLibraryItem, episode: episode)
} else {
return nil
}
}
}