diff --git a/ios/App/Shared/util/Database.swift b/ios/App/Shared/util/Database.swift index c4a35998..6b72cd86 100644 --- a/ios/App/Shared/util/Database.swift +++ b/ios/App/Shared/util/Database.swift @@ -20,12 +20,12 @@ class Database { debugPrint(error) } } - + public func setServerConnectionConfig(config: ServerConnectionConfig) { let config = config let realm = try! Realm() let existing: ServerConnectionConfig? = realm.object(ofType: ServerConnectionConfig.self, forPrimaryKey: config.id) - + if let existing = existing { do { try existing.update { @@ -40,19 +40,19 @@ class Database { AbsLogger.error("setServerConn", message: "failed to update server config") debugPrint(error) } - + setLastActiveConfigIndex(index: existing.index) } else { if config.index == 0 { let lastConfig: ServerConnectionConfig? = realm.objects(ServerConnectionConfig.self).last - + if lastConfig != nil { config.index = lastConfig!.index + 1 } else { config.index = 1 } } - + do { try realm.write { realm.add(config) @@ -61,7 +61,7 @@ class Database { AbsLogger.error(message: "failed to save server config") debugPrint(exception) } - + setLastActiveConfigIndex(index: config.index) } } @@ -78,11 +78,11 @@ class Database { debugPrint("Failed to update server connection config token: \(error)") } } - + public func deleteServerConnectionConfig(id: String) { let realm = try! Realm() let config = realm.object(ofType: ServerConnectionConfig.self, forPrimaryKey: id) - + do { try realm.write { if config != nil { @@ -94,27 +94,27 @@ class Database { debugPrint(exception) } } - + public func getServerConnectionConfigs() -> [ServerConnectionConfig] { let realm = try! Realm() return Array(realm.objects(ServerConnectionConfig.self)) } - + public func setLastActiveConfigIndexToNil() { setLastActiveConfigIndex(index: nil) } - + private func setLastActiveConfigIndex(index: Int?) { let realm = try! Realm() do { try realm.write { let existing = realm.objects(ServerConnectionConfigActiveIndex.self).last - + if ( existing?.index != index ) { if let existing = existing { realm.delete(existing) } - + let activeConfig = ServerConnectionConfigActiveIndex() activeConfig.index = index realm.add(activeConfig) @@ -125,12 +125,12 @@ class Database { debugPrint(exception) } } - + public func getLastActiveConfigIndex() -> Int? { let realm = try! Realm() return realm.objects(ServerConnectionConfigActiveIndex.self).first?.index ?? nil } - + public func setDeviceSettings(deviceSettings: DeviceSettings) { let realm = try! Realm() let existing = realm.objects(DeviceSettings.self) @@ -144,7 +144,7 @@ class Database { AbsLogger.error(message: "failed to save device settings") } } - + public func getLocalLibraryItems(mediaType: MediaType? = nil) -> [LocalLibraryItem] { do { let realm = try Realm() @@ -154,7 +154,7 @@ class Database { return [] } } - + public func getLocalLibraryItem(byServerLibraryItemId: String) -> LocalLibraryItem? { do { let realm = try Realm() @@ -164,7 +164,7 @@ class Database { return nil } } - + public func getLocalLibraryItem(localLibraryItemId: String) -> LocalLibraryItem? { do { let realm = try Realm() @@ -174,12 +174,12 @@ class Database { return nil } } - + public func saveLocalLibraryItem(localLibraryItem: LocalLibraryItem) throws { let realm = try Realm() try realm.write { realm.add(localLibraryItem, update: .modified) } } - + public func getLocalFile(localFileId: String) -> LocalFile? { do { let realm = try Realm() @@ -189,7 +189,7 @@ class Database { return nil } } - + public func getDownloadItem(downloadItemId: String) -> DownloadItem? { do { let realm = try Realm() @@ -199,7 +199,7 @@ class Database { return nil } } - + public func getDownloadItem(libraryItemId: String) -> DownloadItem? { do { let realm = try Realm() @@ -209,7 +209,7 @@ class Database { return nil } } - + public func getDownloadItem(downloadItemPartId: String) -> DownloadItem? { do { let realm = try Realm() @@ -219,17 +219,17 @@ class Database { return nil } } - + public func saveDownloadItem(_ downloadItem: DownloadItem) throws { let realm = try Realm() return try realm.write { realm.add(downloadItem, update: .modified) } } - + public func getDeviceSettings() -> DeviceSettings { let realm = try! Realm() return realm.objects(DeviceSettings.self).first ?? getDefaultDeviceSettings() } - + public func getAllLocalMediaProgress() -> [LocalMediaProgress] { do { let realm = try Realm() @@ -239,7 +239,7 @@ class Database { return [] } } - + // For books this will just be the localLibraryItemId for podcast episodes this will be "{localLibraryItemId}-{episodeId}" public func getLocalMediaProgress(localMediaProgressId: String) -> LocalMediaProgress? { do { @@ -250,7 +250,7 @@ class Database { return nil } } - + public func removeLocalMediaProgress(localMediaProgressId: String) throws { let realm = try Realm() try realm.write { @@ -258,7 +258,7 @@ class Database { realm.delete(progress!) } } - + public func getAllPlaybackSessions() -> [PlaybackSession] { do { let realm = try Realm() @@ -268,7 +268,7 @@ class Database { return [] } } - + public func getPlaybackSession(id: String) -> PlaybackSession? { do { let realm = try Realm() @@ -279,12 +279,12 @@ class Database { return nil } } - + public func saveLog(_ log: LogEntry) throws { let realm = try Realm() return try realm.write { realm.add(log) } } - + public func getAllLogs() -> [LogEntry] { do { let realm = try Realm() @@ -294,7 +294,7 @@ class Database { return [] } } - + public func clearLogs() throws { do { let realm = try! Realm() @@ -308,25 +308,13 @@ class Database { throw error } } - + private func cleanExpiredLogs() throws { let realm = try Realm() let numberOfHoursToKeep = 48 let keepLogCutoffMs = Int(Date().addingTimeInterval(TimeInterval(-1 * numberOfHoursToKeep * 3600)).timeIntervalSince1970 * 1000) - - let allLogs = getAllLogs() - var logsRemoved = 0 - try? realm.write { - allLogs.forEach { log in - if log.timestamp < keepLogCutoffMs { - realm.delete(log) - logsRemoved += 1 - } - } - } - - if logsRemoved > 0 { - AbsLogger.info(message: "cleanLogs: Removed \(logsRemoved) logs older than \(numberOfHoursToKeep) hours") + try realm.write { + realm.delete(realm.objects(LogEntry.self).filter("timestamp < %@", keepLogCutoffMs)) } } }