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
+2 -3
View File
@@ -176,10 +176,10 @@ public class AbsDatabase: CAPPlugin {
call.reject("Local media progress not found or created") call.reject("Local media progress not found or created")
return return
} }
localMediaProgress.updateFromServerMediaProgress(serverMediaProgress)
NSLog("syncServerMediaProgressWithLocalMediaProgress: Saving local media progress") NSLog("syncServerMediaProgressWithLocalMediaProgress: Saving local media progress")
Database.shared.saveLocalMediaProgress(localMediaProgress) localMediaProgress.updateFromServerMediaProgress(serverMediaProgress)
call.resolve(try localMediaProgress.asDictionary()) call.resolve(try localMediaProgress.asDictionary())
} catch { } catch {
call.reject("Failed to sync media progress") call.reject("Failed to sync media progress")
@@ -203,7 +203,6 @@ public class AbsDatabase: CAPPlugin {
// Update finished status // Update finished status
localMediaProgress.updateIsFinished(isFinished) localMediaProgress.updateIsFinished(isFinished)
Database.shared.saveLocalMediaProgress(localMediaProgress)
// Build API response // Build API response
let progressDictionary = try? localMediaProgress.asDictionary() let progressDictionary = try? localMediaProgress.asDictionary()
@@ -158,19 +158,24 @@ extension LocalMediaProgress {
} }
static func fetchOrCreateLocalMediaProgress(localMediaProgressId: String?, localLibraryItemId: String?, localEpisodeId: String?) -> LocalMediaProgress? { static func fetchOrCreateLocalMediaProgress(localMediaProgressId: String?, localLibraryItemId: String?, localEpisodeId: String?) -> LocalMediaProgress? {
if let localMediaProgressId = localMediaProgressId { let realm = try! Realm()
// Check if it existing in the database, if not, we need to create it return try! realm.write { () -> LocalMediaProgress? in
if let progress = Database.shared.getLocalMediaProgress(localMediaProgressId: localMediaProgressId) { if let localMediaProgressId = localMediaProgressId {
return progress // 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
} }
} }
} }
@@ -84,7 +84,6 @@ class PlayerProgress {
} }
localMediaProgress.updateFromPlaybackSession(session) localMediaProgress.updateFromPlaybackSession(session)
Database.shared.saveLocalMediaProgress(localMediaProgress)
NSLog("Local progress saved to the database") NSLog("Local progress saved to the database")