mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-07-14 07:54:50 +02:00
Remove blocking realmQueue
This commit is contained in:
parent
849b6303e8
commit
ad802c16ea
4 changed files with 102 additions and 143 deletions
|
@ -21,12 +21,10 @@ func getDefaultDeviceSettings() -> DeviceSettings {
|
||||||
}
|
}
|
||||||
|
|
||||||
func deviceSettingsToJSON(settings: DeviceSettings) -> Dictionary<String, Any> {
|
func deviceSettingsToJSON(settings: DeviceSettings) -> Dictionary<String, Any> {
|
||||||
return Database.realmQueue.sync {
|
|
||||||
return [
|
return [
|
||||||
"disableAutoRewind": settings.disableAutoRewind,
|
"disableAutoRewind": settings.disableAutoRewind,
|
||||||
"enableAltView": settings.enableAltView,
|
"enableAltView": settings.enableAltView,
|
||||||
"jumpBackwardsTime": settings.jumpBackwardsTime,
|
"jumpBackwardsTime": settings.jumpBackwardsTime,
|
||||||
"jumpForwardTime": settings.jumpForwardTime
|
"jumpForwardTime": settings.jumpForwardTime
|
||||||
]
|
]
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,6 @@ struct ServerConnectionConfigActiveIndex: Realmable {
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertServerConnectionConfigToJSON(config: ServerConnectionConfig) -> Dictionary<String, Any> {
|
func convertServerConnectionConfigToJSON(config: ServerConnectionConfig) -> Dictionary<String, Any> {
|
||||||
return Database.realmQueue.sync {
|
|
||||||
return [
|
return [
|
||||||
"id": config.id,
|
"id": config.id,
|
||||||
"name": config.name,
|
"name": config.name,
|
||||||
|
@ -47,5 +46,4 @@ func convertServerConnectionConfigToJSON(config: ServerConnectionConfig) -> Dict
|
||||||
"username": config.username,
|
"username": config.username,
|
||||||
"token": config.token,
|
"token": config.token,
|
||||||
]
|
]
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,26 +9,19 @@ import Foundation
|
||||||
import RealmSwift
|
import RealmSwift
|
||||||
|
|
||||||
class Database {
|
class Database {
|
||||||
// All DB releated actions must be executed on "realm-queue"
|
|
||||||
public static let realmQueue: DispatchQueue = DispatchQueue(label: "realm-queue")
|
|
||||||
public static var shared = {
|
public static var shared = {
|
||||||
realmQueue.sync {
|
|
||||||
return Database()
|
return Database()
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
private var instance: Realm
|
private init() {}
|
||||||
private init() {
|
|
||||||
self.instance = try! Realm(queue: Database.realmQueue)
|
|
||||||
}
|
|
||||||
|
|
||||||
public func setServerConnectionConfig(config: ServerConnectionConfig) {
|
public func setServerConnectionConfig(config: ServerConnectionConfig) {
|
||||||
var config = config
|
var config = config
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
var existing: ServerConnectionConfig? = instance.object(ofType: ServerConnectionConfig.self, forPrimaryKey: config.id)
|
let existing: ServerConnectionConfig? = realm.object(ofType: ServerConnectionConfig.self, forPrimaryKey: config.id)
|
||||||
|
|
||||||
if config.index == 0 {
|
if config.index == 0 {
|
||||||
var lastConfig: ServerConnectionConfig? = instance.objects(ServerConnectionConfig.self).last
|
let lastConfig: ServerConnectionConfig? = realm.objects(ServerConnectionConfig.self).last
|
||||||
|
|
||||||
if lastConfig != nil {
|
if lastConfig != nil {
|
||||||
config.index = lastConfig!.index + 1
|
config.index = lastConfig!.index + 1
|
||||||
|
@ -38,11 +31,11 @@ class Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try instance.write {
|
try realm.write {
|
||||||
if existing != nil {
|
if existing != nil {
|
||||||
instance.delete(existing!)
|
realm.delete(existing!)
|
||||||
}
|
}
|
||||||
instance.add(config)
|
realm.add(config)
|
||||||
}
|
}
|
||||||
} catch(let exception) {
|
} catch(let exception) {
|
||||||
NSLog("failed to save server config")
|
NSLog("failed to save server config")
|
||||||
|
@ -51,16 +44,15 @@ class Database {
|
||||||
|
|
||||||
setLastActiveConfigIndex(index: config.index)
|
setLastActiveConfigIndex(index: config.index)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public func deleteServerConnectionConfig(id: String) {
|
public func deleteServerConnectionConfig(id: String) {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
let config = instance.object(ofType: ServerConnectionConfig.self, forPrimaryKey: id)
|
let config = realm.object(ofType: ServerConnectionConfig.self, forPrimaryKey: id)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try instance.write {
|
try realm.write {
|
||||||
if config != nil {
|
if config != nil {
|
||||||
instance.delete(config!)
|
realm.delete(config!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch(let exception) {
|
} catch(let exception) {
|
||||||
|
@ -68,26 +60,23 @@ class Database {
|
||||||
debugPrint(exception)
|
debugPrint(exception)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public func getServerConnectionConfigs() -> [ServerConnectionConfig] {
|
public func getServerConnectionConfigs() -> [ServerConnectionConfig] {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
return Array(instance.objects(ServerConnectionConfig.self))
|
return Array(realm.objects(ServerConnectionConfig.self))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func setLastActiveConfigIndexToNil() {
|
public func setLastActiveConfigIndexToNil() {
|
||||||
Database.realmQueue.sync {
|
|
||||||
setLastActiveConfigIndex(index: nil)
|
setLastActiveConfigIndex(index: nil)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private func setLastActiveConfigIndex(index: Int?) {
|
private func setLastActiveConfigIndex(index: Int?) {
|
||||||
|
let realm = try! Realm()
|
||||||
do {
|
do {
|
||||||
try instance.write {
|
try realm.write {
|
||||||
var existing = instance.objects(ServerConnectionConfigActiveIndex.self).last ?? ServerConnectionConfigActiveIndex(index: index)
|
var existing = realm.objects(ServerConnectionConfigActiveIndex.self).last ?? ServerConnectionConfigActiveIndex(index: index)
|
||||||
existing.index = index
|
existing.index = index
|
||||||
instance.add(existing, update: .modified)
|
realm.add(existing, update: .modified)
|
||||||
}
|
}
|
||||||
} catch(let exception) {
|
} catch(let exception) {
|
||||||
NSLog("failed to save server config active index")
|
NSLog("failed to save server config active index")
|
||||||
|
@ -96,127 +85,103 @@ class Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getLastActiveConfigIndex() -> Int? {
|
public func getLastActiveConfigIndex() -> Int? {
|
||||||
return Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
return instance.objects(ServerConnectionConfigActiveIndex.self).first?.index ?? nil
|
return realm.objects(ServerConnectionConfigActiveIndex.self).first?.index ?? nil
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func setDeviceSettings(deviceSettings: DeviceSettings) {
|
public func setDeviceSettings(deviceSettings: DeviceSettings) {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
let existing = instance.objects(DeviceSettings.self)
|
let existing = realm.objects(DeviceSettings.self)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try instance.write {
|
try realm.write {
|
||||||
instance.delete(existing)
|
realm.delete(existing)
|
||||||
instance.add(deviceSettings)
|
realm.add(deviceSettings)
|
||||||
}
|
}
|
||||||
} catch(let exception) {
|
} catch {
|
||||||
NSLog("failed to save device settings")
|
NSLog("failed to save device settings")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public func getLocalLibraryItems(mediaType: MediaType? = nil) -> [LocalLibraryItem] {
|
public func getLocalLibraryItems(mediaType: MediaType? = nil) -> [LocalLibraryItem] {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
Array(instance.objects(LocalLibraryItem.self))
|
return Array(realm.objects(LocalLibraryItem.self))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getLocalLibraryItemByLLId(libraryItem: String) -> LocalLibraryItem? {
|
public func getLocalLibraryItemByLLId(libraryItem: String) -> LocalLibraryItem? {
|
||||||
let items = getLocalLibraryItems()
|
let realm = try! Realm()
|
||||||
for item in items {
|
return realm.objects(LocalLibraryItem.self).first(where: { $0.libraryItemId == libraryItem })
|
||||||
if (item.libraryItemId == libraryItem) {
|
|
||||||
return item
|
|
||||||
}
|
|
||||||
}
|
|
||||||
NSLog("Local library item with id \(libraryItem) not found")
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getLocalLibraryItem(localLibraryItem: String) -> LocalLibraryItem? {
|
public func getLocalLibraryItem(localLibraryItem: String) -> LocalLibraryItem? {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
instance.object(ofType: LocalLibraryItem.self, forPrimaryKey: localLibraryItem)
|
return realm.object(ofType: LocalLibraryItem.self, forPrimaryKey: localLibraryItem)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func saveLocalLibraryItem(localLibraryItem: LocalLibraryItem) {
|
public func saveLocalLibraryItem(localLibraryItem: LocalLibraryItem) {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
try! instance.write { instance.add(localLibraryItem) }
|
try! realm.write { realm.add(localLibraryItem) }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getDownloadItem(downloadItemId: String) -> DownloadItem? {
|
public func getDownloadItem(downloadItemId: String) -> DownloadItem? {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
instance.object(ofType: DownloadItem.self, forPrimaryKey: downloadItemId)
|
return realm.object(ofType: DownloadItem.self, forPrimaryKey: downloadItemId)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getDownloadItem(libraryItemId: String) -> DownloadItem? {
|
public func getDownloadItem(libraryItemId: String) -> DownloadItem? {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
instance.objects(DownloadItem.self).filter("libraryItemId == %@", libraryItemId).first
|
return realm.objects(DownloadItem.self).filter("libraryItemId == %@", libraryItemId).first
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getDownloadItem(downloadItemPartId: String) -> DownloadItem? {
|
public func getDownloadItem(downloadItemPartId: String) -> DownloadItem? {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
instance.objects(DownloadItem.self).filter("SUBQUERY(downloadItemParts, $part, $part.id == %@) .@count > 0", downloadItemPartId).first
|
return realm.objects(DownloadItem.self).filter("SUBQUERY(downloadItemParts, $part, $part.id == %@) .@count > 0", downloadItemPartId).first
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func saveDownloadItem(_ downloadItem: DownloadItem) {
|
public func saveDownloadItem(_ downloadItem: DownloadItem) {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
try! instance.write { instance.add(downloadItem) }
|
return try! realm.write { realm.add(downloadItem) }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func updateDownloadItemPart(_ part: DownloadItemPart) {
|
public func updateDownloadItemPart(_ part: DownloadItemPart) {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
try! instance.write {
|
return try! realm.write { realm.add(part, update: .modified) }
|
||||||
instance.add(part, update: .modified)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func removeDownloadItem(_ downloadItem: DownloadItem) {
|
public func removeDownloadItem(_ downloadItem: DownloadItem) {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
try! instance.write { instance.delete(downloadItem) }
|
return try! realm.write { realm.delete(downloadItem) }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getDeviceSettings() -> DeviceSettings {
|
public func getDeviceSettings() -> DeviceSettings {
|
||||||
return Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
return instance.objects(DeviceSettings.self).first ?? getDefaultDeviceSettings()
|
return realm.objects(DeviceSettings.self).first ?? getDefaultDeviceSettings()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func removeLocalLibraryItem(localLibraryItemId: String) {
|
public func removeLocalLibraryItem(localLibraryItemId: String) {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
try! instance.write {
|
try! realm.write {
|
||||||
let item = getLocalLibraryItemByLLId(libraryItem: localLibraryItemId)
|
let item = getLocalLibraryItemByLLId(libraryItem: localLibraryItemId)
|
||||||
instance.delete(item!)
|
realm.delete(item!)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func saveLocalMediaProgress(_ mediaProgress: LocalMediaProgress) {
|
public func saveLocalMediaProgress(_ mediaProgress: LocalMediaProgress) {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
try! instance.write { instance.add(mediaProgress) }
|
try! realm.write { realm.add(mediaProgress) }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// For books this will just be the localLibraryItemId for podcast episodes this will be "{localLibraryItemId}-{episodeId}"
|
// For books this will just be the localLibraryItemId for podcast episodes this will be "{localLibraryItemId}-{episodeId}"
|
||||||
public func getLocalMediaProgress(localMediaProgressId: String) -> LocalMediaProgress? {
|
public func getLocalMediaProgress(localMediaProgressId: String) -> LocalMediaProgress? {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
instance.object(ofType: LocalMediaProgress.self, forPrimaryKey: localMediaProgressId)
|
return realm.object(ofType: LocalMediaProgress.self, forPrimaryKey: localMediaProgressId)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func removeLocalMediaProgress(localMediaProgressId: String) {
|
public func removeLocalMediaProgress(localMediaProgressId: String) {
|
||||||
Database.realmQueue.sync {
|
let realm = try! Realm()
|
||||||
try! instance.write {
|
try! realm.write {
|
||||||
let progress = instance.object(ofType: LocalMediaProgress.self, forPrimaryKey: localMediaProgressId)
|
let progress = realm.object(ofType: LocalMediaProgress.self, forPrimaryKey: localMediaProgressId)
|
||||||
instance.delete(progress!)
|
realm.delete(progress!)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,9 +21,7 @@ class Store {
|
||||||
Database.shared.setLastActiveConfigIndexToNil()
|
Database.shared.setLastActiveConfigIndexToNil()
|
||||||
}
|
}
|
||||||
|
|
||||||
Database.realmQueue.sync {
|
|
||||||
_serverConfig = updated
|
_serverConfig = updated
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue