Fix server not sending updates every 10 seconds

This commit is contained in:
ronaldheft
2022-08-23 17:32:43 -04:00
parent 6a885b7241
commit 099be648bf
3 changed files with 18 additions and 8 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// Override point for customization after application launch. // Override point for customization after application launch.
let configuration = Realm.Configuration( let configuration = Realm.Configuration(
schemaVersion: 2, schemaVersion: 3,
migrationBlock: { migration, oldSchemaVersion in migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) { if (oldSchemaVersion < 1) {
NSLog("Realm schema version was \(oldSchemaVersion)") NSLog("Realm schema version was \(oldSchemaVersion)")
@@ -31,6 +31,7 @@ class PlaybackSession: Object, Codable, Deletable {
@Persisted var serverConnectionConfigId: String? @Persisted var serverConnectionConfigId: String?
@Persisted var serverAddress: String? @Persisted var serverAddress: String?
@Persisted var isActiveSession = true @Persisted var isActiveSession = true
@Persisted var serverUpdatedAt: Double = 0
var isLocal: Bool { self.localLibraryItem != nil } var isLocal: Bool { self.localLibraryItem != nil }
var mediaPlayer: String { "AVPlayer" } var mediaPlayer: String { "AVPlayer" }
+16 -7
View File
@@ -96,11 +96,12 @@ class PlayerProgress {
} }
private func updateServerSessionFromLocalSession(_ session: PlaybackSession, rateLimitSync: Bool = false) async { private func updateServerSessionFromLocalSession(_ session: PlaybackSession, rateLimitSync: Bool = false) async {
let nowInMilliseconds = Date().timeIntervalSince1970 * 1000
// If required, rate limit requests based on session last update // If required, rate limit requests based on session last update
if rateLimitSync { if rateLimitSync {
let now = Date().timeIntervalSince1970 * 1000 let lastUpdateInMilliseconds = session.serverUpdatedAt
let lastUpdate = session.updatedAt ?? now let timeSinceLastSync = nowInMilliseconds - lastUpdateInMilliseconds
let timeSinceLastSync = now - lastUpdate
let timeBetweenSessionSync = PlayerProgress.TIME_BETWEEN_SESSION_SYNC_IN_SECONDS * 1000 let timeBetweenSessionSync = PlayerProgress.TIME_BETWEEN_SESSION_SYNC_IN_SECONDS * 1000
guard timeSinceLastSync > timeBetweenSessionSync else { guard timeSinceLastSync > timeBetweenSessionSync else {
// Skipping sync since last occurred within session sync time // Skipping sync since last occurred within session sync time
@@ -118,10 +119,18 @@ class PlayerProgress {
success = await ApiClient.reportPlaybackProgress(report: playbackReport, sessionId: session.id) success = await ApiClient.reportPlaybackProgress(report: playbackReport, sessionId: session.id)
} }
// Remove old sessions after they synced with the server if success {
if success && !session.isActiveSession { if let session = session.thaw() {
NSLog("Deleting sessionId(\(session.id)) as is no longer active") // Update the server sync time, which is different than lastUpdate
session.thaw()?.delete() session.update {
session.serverUpdatedAt = nowInMilliseconds
}
// Remove old sessions after they synced with the server
if !session.isActiveSession {
session.delete()
}
}
} }
} }