diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index ddaba8a..af4b0b2 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -15,7 +15,6 @@ public enum StoreError: Error, LocalizedError { case missingToken case missingKeychainStore case collectionNotRegistered(type: String) - case cannotSyncCollection(name: String) case apiCallCollectionNotRegistered(type: String) public var errorDescription: String? { @@ -32,8 +31,6 @@ public enum StoreError: Error, LocalizedError { return "There is no keychain store" case .collectionNotRegistered(let type): return "The collection \(type) is not registered" - case .cannotSyncCollection(let name): - return "Tries to load the collection \(name) from the server while it's not authorized" case .apiCallCollectionNotRegistered(let type): return "The api call collection has not been registered for \(type)" } @@ -127,9 +124,10 @@ final public class Store { /// - synchronized: indicates if the data is synchronized with the server /// - inMemory: Indicates if the collection should only live in memory, and not write into a file /// - sendsUpdate: Indicates if updates of items should be sent to the server - public func registerObject(synchronized: Bool, inMemory: Bool = false, sendsUpdate: Bool = true) -> StoredSingleton { + public func registerObject(synchronized: Bool, inMemory: Bool = false, shouldLoadDataFromServer: Bool = true) -> StoredSingleton { - let storedObject = StoredSingleton(store: self, inMemory: inMemory) + let storedObject = StoredSingleton(store: self, inMemory: inMemory, shouldLoadDataFromServer: shouldLoadDataFromServer) + self._collections[T.resourceName()] = storedObject self._collections[T.resourceName()] = storedObject if synchronized { diff --git a/LeStorage/StoreCenter.swift b/LeStorage/StoreCenter.swift index d026cec..5c77c9a 100644 --- a/LeStorage/StoreCenter.swift +++ b/LeStorage/StoreCenter.swift @@ -64,6 +64,9 @@ public class StoreCenter { /// Used for testing, gives the project name to retrieve classes from names var classProject: String? = nil + + var useWebsockets: Bool = false + var useSynchronization: Bool = false init(directoryName: String? = nil) { @@ -87,22 +90,31 @@ public class StoreCenter { } public func configureURLs(secureScheme: Bool, domain: String, webSockets: Bool = true) { + + self.useWebsockets = webSockets + let urlManager: URLManager = URLManager(secureScheme: secureScheme, domain: domain) self._urlManager = urlManager self._services = Services(storeCenter: self, url: urlManager.api) self.tokenKeychain = KeychainStore(serverId: urlManager.api) - self._dataAccess = self.mainStore.registerSynchronizedCollection() + if self.useSynchronization { + self._dataAccess = self.mainStore.registerSynchronizedCollection() + } Logger.log("Sync URL: \(urlManager.api)") - if webSockets && self.userId != nil { + if self.userId != nil { self._configureWebSocket() } } fileprivate func _configureWebSocket() { + guard self.useWebsockets else { + return + } + self._webSocketManager?.disconnect() self._webSocketManager = nil @@ -540,11 +552,13 @@ public class StoreCenter { self.mainStore.loadCollectionsFromServer(clear: clear) // request data that has been shared with the user - Task { - do { - try await self.service().getUserDataAccess() - } catch { - Logger.error(error) + if self.useSynchronization { + Task { + do { + try await self.service().getUserDataAccess() + } catch { + Logger.error(error) + } } } diff --git a/LeStorage/StoredSingleton.swift b/LeStorage/StoredSingleton.swift index 2f59e94..1ef5dc4 100644 --- a/LeStorage/StoredSingleton.swift +++ b/LeStorage/StoredSingleton.swift @@ -10,6 +10,19 @@ import Foundation /// A class extending the capabilities of StoredCollection but supposedly manages only one item public class StoredSingleton: SyncedCollection { + var shouldLoadDataFromServer: Bool = true + + init(store: Store, inMemory: Bool = false, shouldLoadDataFromServer: Bool = true) { + super.init(store: store, inMemory: inMemory) + self.shouldLoadDataFromServer = shouldLoadDataFromServer + } + + public override func loadDataFromServerIfAllowed(clear: Bool = false) async throws { + if shouldLoadDataFromServer { + try await super.loadDataFromServerIfAllowed(clear: clear) + } + } + /// Sets the singleton to the collection without synchronizing it public func setItemNoSync(_ instance: T) { self.setSingletonNoSync(instance: instance) diff --git a/LeStorage/SyncedCollection.swift b/LeStorage/SyncedCollection.swift index 72f4c68..a87709c 100644 --- a/LeStorage/SyncedCollection.swift +++ b/LeStorage/SyncedCollection.swift @@ -46,9 +46,6 @@ public class SyncedCollection: BaseCollection, SomeSynced /// Retrieves the data from the server and loads it into the items array public func loadDataFromServerIfAllowed(clear: Bool = false) async throws { - guard !(self is StoredSingleton) else { - throw StoreError.cannotSyncCollection(name: self.resourceName) - } do { try await self.storeCenter.sendGetRequest(T.self, storeId: self.storeId, clear: clear) } catch {