From c32c01568bf6cf37b7b40f00e9339af319307271 Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 2 Dec 2024 11:04:27 +0100 Subject: [PATCH] Adds limits for logs and failed api call + method to reset them --- LeStorage/Store.swift | 4 ++-- LeStorage/StoreCenter.swift | 19 +++++++++++++++++-- LeStorage/StoredCollection.swift | 14 +++++++++++++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index f023778..8cf84c5 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -88,9 +88,9 @@ open class Store { /// - indexed: Creates an index to quickly access the data /// - 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 registerCollection(synchronized: Bool, indexed: Bool = false, inMemory: Bool = false, sendsUpdate: Bool = true) -> StoredCollection { + public func registerCollection(synchronized: Bool, indexed: Bool = false, inMemory: Bool = false, sendsUpdate: Bool = true, limit: Int? = nil) -> StoredCollection { - let collection = StoredCollection(synchronized: synchronized, store: self, indexed: indexed, inMemory: inMemory, sendsUpdate: sendsUpdate) + let collection = StoredCollection(synchronized: synchronized, store: self, indexed: indexed, inMemory: inMemory, sendsUpdate: sendsUpdate, limit: limit) self._collections[T.resourceName()] = collection if synchronized { diff --git a/LeStorage/StoreCenter.swift b/LeStorage/StoreCenter.swift index 3fe6302..3d351ef 100644 --- a/LeStorage/StoreCenter.swift +++ b/LeStorage/StoreCenter.swift @@ -251,6 +251,21 @@ public class StoreCenter { } } + public func resetLoggingCollections() { + self._failedAPICallsCollection?.reset() + self._logs?.reset() + Task { + do { + let facApiCallCollection: ApiCallCollection = try self.apiCallCollection() + await facApiCallCollection.reset() + let logApiCallCollection: ApiCallCollection = try self.apiCallCollection() + await logApiCallCollection.reset() + } catch { + Logger.error(error) + } + } + } + /// Resets the ApiCall whose type identifies with the provided collection /// - Parameters: /// - collection: The collection identifying the Storable type @@ -282,7 +297,7 @@ public class StoreCenter { /// This method triggers the framework to save and send failed api calls public func logsFailedAPICalls() { - self._failedAPICallsCollection = Store.main.registerCollection(synchronized: true) + self._failedAPICallsCollection = Store.main.registerCollection(synchronized: true, limit: 50) } /// If configured for, logs and send to the server a failed API call @@ -447,7 +462,7 @@ public class StoreCenter { if let logs = self._logs { return logs } else { - let logsCollection: StoredCollection = Store.main.registerCollection(synchronized: true) + let logsCollection: StoredCollection = Store.main.registerCollection(synchronized: true, limit: 50) self._logs = logsCollection return logsCollection } diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index e04650c..ac0b30e 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -71,7 +71,9 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti /// Indicates if the collection has loaded locally, with or without a file fileprivate(set) public var hasLoaded: Bool = false - init(synchronized: Bool, store: Store, indexed: Bool = false, asynchronousIO: Bool = true, inMemory: Bool = false, sendsUpdate: Bool = true) { + fileprivate(set) var limit: Int? = nil + + init(synchronized: Bool, store: Store, indexed: Bool = false, asynchronousIO: Bool = true, inMemory: Bool = false, sendsUpdate: Bool = true, limit: Int? = nil) { self.synchronized = synchronized self.asynchronousIO = asynchronousIO if indexed { @@ -80,6 +82,7 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti self._inMemory = inMemory self._sendsUpdate = sendsUpdate self._store = store + self.limit = limit self._load() } @@ -222,11 +225,18 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti } else { // insert self.items.append(instance) self._sendInsertionIfNecessary(instance) + self._applyLimitIfPresent() } self._indexes?[instance.id] = instance } + fileprivate func _applyLimitIfPresent() { + if let limit { + self.items = self.items.suffix(limit) + } + } + /// Sends a POST request for the instance, and changes the collection to perform a write public func writeChangeAndInsertOnServer(instance: T) { defer { @@ -308,6 +318,8 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti self._indexes?[instance.id] = instance } + self._applyLimitIfPresent() + } /// Returns the instance corresponding to the provided [id]