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

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

@ -145,8 +145,10 @@ open class Store {
/// Loads all collection with the data from the server
public func loadCollectionFromServer() {
for collection in self._collections.values {
Task {
try? await collection.loadDataFromServerIfAllowed()
if collection.synchronized {
Task {
try? await collection.loadDataFromServerIfAllowed()
}
}
}
}
@ -239,11 +241,13 @@ open class Store {
public func loadCollectionsFromServerIfNoFile() {
for collection in self._collections.values {
// Logger.log("Load \(name)")
Task {
do {
try await collection.loadCollectionsFromServerIfNoFile()
} catch {
Logger.error(error)
if collection.synchronized {
Task {
do {
try await collection.loadCollectionsFromServerIfNoFile()
} catch {
Logger.error(error)
}
}
}
}

@ -53,15 +53,9 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
/// The reference to the 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
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
fileprivate var _hasChanged: Bool = false {
didSet {
@ -338,16 +332,12 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
self.items.remove(at: index)
}
// self.items.removeAll(where: { $0.id == item.id })
Task {
do {
try await StoreCenter.main.deleteApiCallByDataId(type: T.self, id: item.stringId)
} catch {
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)
}
// 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
/// Returns the collection items as [any Storable]
@ -403,7 +377,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
do {
let jsonString: String = try self.items.jsonString()
try self._store.write(content: jsonString, fileName: T.fileName())
// try T.writeToStorageDirectory(content: jsonString, fileName: T.fileName())
} catch {
Logger.error(error) // TODO how to notify the main project
}
@ -431,15 +404,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
// self.resetApiCalls()
}
// /// Removes the collection related API calls collection
// public func resetApiCalls() {
// if let apiCallsCollection = self.apiCallsCollection {
// Task {
// await apiCallsCollection.reset()
// }
// }
// }
// MARK: - Reschedule calls
/// 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
public var startIndex: Int { return self.items.startIndex }

@ -1,19 +1,19 @@
# LeStorage
**1. RULES**
# Rules
- To store data in the json format inside files,
you first need to create some model class, for example `Car`
- You make `Car` inherit `ModelObject`, and implement `Storable`
- 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
- 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
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 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:
- Set `StoreCenter.main.synchronizationApiURL`

Loading…
Cancel
Save