Adds limits for logs and failed api call + method to reset them

sync2
Laurent 11 months ago
parent d3431c0c55
commit c32c01568b
  1. 4
      LeStorage/Store.swift
  2. 19
      LeStorage/StoreCenter.swift
  3. 14
      LeStorage/StoredCollection.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<T : Storable>(synchronized: Bool, indexed: Bool = false, inMemory: Bool = false, sendsUpdate: Bool = true) -> StoredCollection<T> {
public func registerCollection<T : Storable>(synchronized: Bool, indexed: Bool = false, inMemory: Bool = false, sendsUpdate: Bool = true, limit: Int? = nil) -> StoredCollection<T> {
let collection = StoredCollection<T>(synchronized: synchronized, store: self, indexed: indexed, inMemory: inMemory, sendsUpdate: sendsUpdate)
let collection = StoredCollection<T>(synchronized: synchronized, store: self, indexed: indexed, inMemory: inMemory, sendsUpdate: sendsUpdate, limit: limit)
self._collections[T.resourceName()] = collection
if synchronized {

@ -251,6 +251,21 @@ public class StoreCenter {
}
}
public func resetLoggingCollections() {
self._failedAPICallsCollection?.reset()
self._logs?.reset()
Task {
do {
let facApiCallCollection: ApiCallCollection<FailedAPICall> = try self.apiCallCollection()
await facApiCallCollection.reset()
let logApiCallCollection: ApiCallCollection<Log> = 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<Log> = Store.main.registerCollection(synchronized: true)
let logsCollection: StoredCollection<Log> = Store.main.registerCollection(synchronized: true, limit: 50)
self._logs = logsCollection
return logsCollection
}

@ -71,7 +71,9 @@ public class StoredCollection<T: Storable>: 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<T: Storable>: RandomAccessCollection, SomeCollecti
self._inMemory = inMemory
self._sendsUpdate = sendsUpdate
self._store = store
self.limit = limit
self._load()
}
@ -222,11 +225,18 @@ public class StoredCollection<T: Storable>: 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<T: Storable>: RandomAccessCollection, SomeCollecti
self._indexes?[instance.id] = instance
}
self._applyLimitIfPresent()
}
/// Returns the instance corresponding to the provided [id]

Loading…
Cancel
Save