Fix unsync-ed collection trying to load from server + doc

sync
Laurent 1 year ago
parent 3753e09e16
commit a2283fce6d
  1. 4
      LeStorage/Store.swift
  2. 43
      LeStorage/StoredCollection.swift
  3. 12
      README.md

@ -145,11 +145,13 @@ open class Store {
/// Loads all collection with the data from the server /// Loads all collection with the data from the server
public func loadCollectionFromServer() { public func loadCollectionFromServer() {
for collection in self._collections.values { for collection in self._collections.values {
if collection.synchronized {
Task { Task {
try? await collection.loadDataFromServerIfAllowed() try? await collection.loadDataFromServerIfAllowed()
} }
} }
} }
}
/// Resets all registered collection /// Resets all registered collection
public func reset() { public func reset() {
@ -239,6 +241,7 @@ open class Store {
public func loadCollectionsFromServerIfNoFile() { public func loadCollectionsFromServerIfNoFile() {
for collection in self._collections.values { for collection in self._collections.values {
// Logger.log("Load \(name)") // Logger.log("Load \(name)")
if collection.synchronized {
Task { Task {
do { do {
try await collection.loadCollectionsFromServerIfNoFile() try await collection.loadCollectionsFromServerIfNoFile()
@ -248,6 +251,7 @@ open class Store {
} }
} }
} }
}
fileprivate var _validIds: [String] = [] fileprivate var _validIds: [String] = []

@ -53,15 +53,9 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
/// The reference to the Store /// The reference to the Store
fileprivate var _store: Store fileprivate var _store: Store
/// Notifies the closure when the loading is done
// fileprivate var loadCompletion: ((StoredCollection<T>) -> ())? = nil
/// Provides fast access for instances if the collection has been instanced with [indexed] = true /// Provides fast access for instances if the collection has been instanced with [indexed] = true
fileprivate var _indexes: [String : T]? = nil fileprivate var _indexes: [String : T]? = nil
/// Collection of API calls used to store HTTP calls
// fileprivate var apiCallsCollection: ApiCallCollection<T>? = nil
/// Indicates whether the collection has changed, thus requiring a write operation /// Indicates whether the collection has changed, thus requiring a write operation
fileprivate var _hasChanged: Bool = false { fileprivate var _hasChanged: Bool = false {
didSet { didSet {
@ -338,16 +332,12 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
self.items.remove(at: index) self.items.remove(at: index)
} }
// self.items.removeAll(where: { $0.id == item.id })
Task { Task {
do { do {
try await StoreCenter.main.deleteApiCallByDataId(type: T.self, id: item.stringId) try await StoreCenter.main.deleteApiCallByDataId(type: T.self, id: item.stringId)
} catch { } catch {
Logger.error(error) Logger.error(error)
} }
/// Remove related API call if existing
// await self.apiCallsCollection?.deleteByDataId(item.stringId)
} }
} }
@ -358,22 +348,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
try self.delete(contentOfs: self.items) try self.delete(contentOfs: self.items)
} }
// MARK: - Some Collection
/// Deletes an API Call by its id
/// - Parameters:
/// - id: the id of the API Call
// func deleteApiCallById(_ id: String) async throws {
// await self.apiCallsCollection?.deleteById(id)
// }
//
// /// Returns an API Call by its id
// /// - Parameters:
// /// - id: the id of the API Call
// func apiCallById(_ id: String) async -> (any SomeCall)? {
// return await self.apiCallsCollection?.findById(id)
// }
// MARK: - SomeCall // MARK: - SomeCall
/// Returns the collection items as [any Storable] /// Returns the collection items as [any Storable]
@ -403,7 +377,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
do { do {
let jsonString: String = try self.items.jsonString() let jsonString: String = try self.items.jsonString()
try self._store.write(content: jsonString, fileName: T.fileName()) try self._store.write(content: jsonString, fileName: T.fileName())
// try T.writeToStorageDirectory(content: jsonString, fileName: T.fileName())
} catch { } catch {
Logger.error(error) // TODO how to notify the main project Logger.error(error) // TODO how to notify the main project
} }
@ -431,15 +404,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
// self.resetApiCalls() // self.resetApiCalls()
} }
// /// Removes the collection related API calls collection
// public func resetApiCalls() {
// if let apiCallsCollection = self.apiCallsCollection {
// Task {
// await apiCallsCollection.reset()
// }
// }
// }
// MARK: - Reschedule calls // MARK: - Reschedule calls
/// Sends an insert api call for the provided [instance] /// Sends an insert api call for the provided [instance]
@ -478,13 +442,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
} }
} }
/// Reschedule the api calls if possible
// func rescheduleApiCallsIfNecessary() {
// Task {
// await self.apiCallsCollection?.rescheduleApiCallsIfNecessary()
// }
// }
// MARK: - RandomAccessCollection // MARK: - RandomAccessCollection
public var startIndex: Int { return self.items.startIndex } public var startIndex: Int { return self.items.startIndex }

@ -1,19 +1,19 @@
# LeStorage # LeStorage
**1. RULES** # Rules
- To store data in the json format inside files, - To store data in the json format inside files,
you first need to create some model class, for example `Car` you first need to create some model class, for example `Car`
- You make `Car` inherit `ModelObject`, and implement `Storable` - You make `Car` inherit `ModelObject`, and implement `Storable`
- To get the `StoredCollection` that manages all your cars and stores them for you, you do - To get the `StoredCollection` that manages all your cars and stores them for you, you do
`Store.main.registerCollection()` to retrieve a collection. `Store.main.registerCollection()` to retrieve a collection. LeStorage stores data as JSON files inside the **storage** directory.
**A. Multi Store** ## Multi Store
You can store collections inside separate folders by creating other stores You can store collections inside separate folders by creating other stores:
- Use StoreCenter.main.store(identifier: id, parameter: param) to create a new store. The directory will be named after the identifier. The parameter is used to retrieve data from server as the GET requests will add the parameter as an argument in the URL, like https://www.myurl.net/api/cars/?param=id - Use StoreCenter.main.store(identifier: id, parameter: param) to create a new store. The directory will be named after the identifier under the **storage** directory. The parameter is used to retrieve data from server as the GET requests will add the parameter as an argument in the URL, like https://www.myurl.net/api/cars/?param=id
**2. Sync** # Sync
- When registering your collection, you can choose to have it synchronized. To do that: - When registering your collection, you can choose to have it synchronized. To do that:
- Set `StoreCenter.main.synchronizationApiURL` - Set `StoreCenter.main.synchronizationApiURL`

Loading…
Cancel
Save