|
|
|
@ -19,7 +19,7 @@ public enum StoreError: Error { |
|
|
|
case unexpectedCollectionType(name: String) |
|
|
|
case unexpectedCollectionType(name: String) |
|
|
|
case apiCallCollectionNotRegistered(type: String) |
|
|
|
case apiCallCollectionNotRegistered(type: String) |
|
|
|
case collectionNotRegistered(type: String) |
|
|
|
case collectionNotRegistered(type: String) |
|
|
|
case unSynchronizedCollection |
|
|
|
case cannotSyncCollection(name: String) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public struct StoreIdentifier { |
|
|
|
public struct StoreIdentifier { |
|
|
|
@ -82,6 +82,10 @@ open class Store { |
|
|
|
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) |
|
|
|
self._collections[T.resourceName()] = collection |
|
|
|
self._collections[T.resourceName()] = collection |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if synchronized { |
|
|
|
|
|
|
|
StoreCenter.main.loadApiCallCollection(type: T.self) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if self._created, let identifier { |
|
|
|
if self._created, let identifier { |
|
|
|
self._migrate(collection, identifier: identifier, type: T.self) |
|
|
|
self._migrate(collection, identifier: identifier, type: T.self) |
|
|
|
} |
|
|
|
} |
|
|
|
@ -227,6 +231,19 @@ open class Store { |
|
|
|
try await StoreCenter.main.sendDeletion(instance) |
|
|
|
try await StoreCenter.main.sendDeletion(instance) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public func loadCollectionsFromServerIfNoFile() { |
|
|
|
|
|
|
|
for (name, collection) in self._collections { |
|
|
|
|
|
|
|
// Logger.log("Load \(name)") |
|
|
|
|
|
|
|
Task { |
|
|
|
|
|
|
|
do { |
|
|
|
|
|
|
|
try await collection.loadCollectionsFromServerIfNoFile() |
|
|
|
|
|
|
|
} catch { |
|
|
|
|
|
|
|
Logger.error(error) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fileprivate var _validIds: [String] = [] |
|
|
|
fileprivate var _validIds: [String] = [] |
|
|
|
|
|
|
|
|
|
|
|
fileprivate func _migrate<T : Storable>(_ collection: StoredCollection<T>, identifier: StoreIdentifier, type: T.Type) { |
|
|
|
fileprivate func _migrate<T : Storable>(_ collection: StoredCollection<T>, identifier: StoreIdentifier, type: T.Type) { |
|
|
|
@ -236,7 +253,6 @@ open class Store { |
|
|
|
let oldCollection: StoredCollection<T> = StoredCollection<T>(synchronized: false, store: Store.main, asynchronousIO: false) |
|
|
|
let oldCollection: StoredCollection<T> = StoredCollection<T>(synchronized: false, store: Store.main, asynchronousIO: false) |
|
|
|
|
|
|
|
|
|
|
|
let filtered: [T] = oldCollection.items.filter { item in |
|
|
|
let filtered: [T] = oldCollection.items.filter { item in |
|
|
|
|
|
|
|
|
|
|
|
var propertyValue: String? = item.stringForPropertyName(identifier.parameterName) |
|
|
|
var propertyValue: String? = item.stringForPropertyName(identifier.parameterName) |
|
|
|
if propertyValue == nil { |
|
|
|
if propertyValue == nil { |
|
|
|
let values = T.relationshipNames.map { item.stringForPropertyName($0) } |
|
|
|
let values = T.relationshipNames.map { item.stringForPropertyName($0) } |
|
|
|
@ -244,11 +260,13 @@ open class Store { |
|
|
|
} |
|
|
|
} |
|
|
|
return self._validIds.first(where: { $0 == propertyValue }) != nil |
|
|
|
return self._validIds.first(where: { $0 == propertyValue }) != nil |
|
|
|
} |
|
|
|
} |
|
|
|
self._validIds.append(contentsOf: filtered.map { $0.stringId }) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if filtered.count > 0 { |
|
|
|
|
|
|
|
self._validIds.append(contentsOf: filtered.map { $0.stringId }) |
|
|
|
try? collection.addOrUpdateNoSync(contentOfs: filtered) |
|
|
|
try? collection.addOrUpdateNoSync(contentOfs: filtered) |
|
|
|
Logger.log("Migrated \(filtered.count) \(T.resourceName())") |
|
|
|
Logger.log("Migrated \(filtered.count) \(T.resourceName())") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|