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

@ -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()

View file

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

View file

@ -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")