Fixes issues

sync3
Laurent 6 months ago
parent 9efc8b14c8
commit 27bb855c8d
  1. 8
      LeStorage/Services.swift
  2. 1
      LeStorage/Store.swift
  3. 38
      LeStorage/StoreCenter.swift
  4. 5
      LeStorage/StoredCollection.swift

@ -35,8 +35,8 @@ let postDeviceTokenCall: ServiceCall = ServiceCall(
path: "device-token/", method: .post, requiresToken: true) path: "device-token/", method: .post, requiresToken: true)
let getUserDataAccessCallContent: ServiceCall = ServiceCall( let getUserDataAccessCallContent: ServiceCall = ServiceCall(
path: "data-access-content/", method: .get, requiresToken: true) path: "data-access-content/", method: .get, requiresToken: true)
let userNamesCall: ServiceCall = ServiceCall( let userAgentsCall: ServiceCall = ServiceCall(
path: "user-names/", method: .get, requiresToken: true) path: "user-agents/", method: .get, requiresToken: true)
/// A class used to send HTTP request to the django server /// A class used to send HTTP request to the django server
public class Services { public class Services {
@ -564,8 +564,8 @@ public class Services {
// MARK: - Others // MARK: - Others
public func getUserNames() async throws -> [ShortUser] { public func getUserAgents() async throws -> [ShortUser] {
return try await self._runRequest(serviceCall: userNamesCall) return try await self._runRequest(serviceCall: userAgentsCall)
} }
// MARK: - Authentication // MARK: - Authentication

@ -252,6 +252,7 @@ final public class Store {
// MARK: - Synchronization // MARK: - Synchronization
/// Calls addOrUpdateIfNewer from the collection corresponding to the instance /// Calls addOrUpdateIfNewer from the collection corresponding to the instance
@MainActor
func addOrUpdateIfNewer<T: SyncedStorable>(_ instance: T, shared: Bool) { func addOrUpdateIfNewer<T: SyncedStorable>(_ instance: T, shared: Bool) {
let collection: SyncedCollection<T> = self.registerOrGetSyncedCollection(T.self) let collection: SyncedCollection<T> = self.registerOrGetSyncedCollection(T.self)
collection.addOrUpdateIfNewer(instance, shared: shared) collection.addOrUpdateIfNewer(instance, shared: shared)

@ -62,8 +62,8 @@ public class StoreCenter {
/// The URL manager /// The URL manager
fileprivate var _urlManager: URLManager? = nil fileprivate var _urlManager: URLManager? = nil
/// Used for testing, gives the project name to retrieve classes from names /// Gives the project name to retrieve classes from names
var classProject: String? = nil public var classProject: String? = nil
var useWebsockets: Bool = false var useWebsockets: Bool = false
var useSynchronization: Bool = false var useSynchronization: Bool = false
@ -640,14 +640,14 @@ public class StoreCenter {
/// Processes the data coming from a sync request /// Processes the data coming from a sync request
@MainActor func synchronizeContent(_ syncData: SyncData) async { @MainActor func synchronizeContent(_ syncData: SyncData) async {
self._syncAddOrUpdate(syncData.updates) await self._syncAddOrUpdate(syncData.updates)
await self._syncDelete(syncData.deletions) await self._syncDelete(syncData.deletions)
self._syncAddOrUpdate(syncData.grants, shared: true) await self._syncAddOrUpdate(syncData.grants, shared: true)
await self.syncRevoke(syncData.revocations, parents: syncData.revocationParents) await self.syncRevoke(syncData.revocations, parents: syncData.revocationParents)
// self._syncAddOrUpdate(syncData.relationshipSets) // self._syncAddOrUpdate(syncData.relationshipSets)
// await self._syncDelete(syncData.relationshipRemovals) // await self._syncDelete(syncData.relationshipRemovals)
self._syncAddOrUpdate(syncData.sharedRelationshipSets, shared: true) await self._syncAddOrUpdate(syncData.sharedRelationshipSets, shared: true)
self._syncRevoke(syncData.sharedRelationshipRemovals) await self._syncRevoke(syncData.sharedRelationshipRemovals)
Logger.log("sync content: updates = \(syncData.updates.count) / deletions = \(syncData.deletions.count), grants = \(syncData.grants.count)") Logger.log("sync content: updates = \(syncData.updates.count) / deletions = \(syncData.deletions.count), grants = \(syncData.grants.count)")
@ -667,12 +667,13 @@ public class StoreCenter {
/// - Parameters: /// - Parameters:
/// - updateArrays: the server updates /// - updateArrays: the server updates
/// - shared: indicates if the content should be flagged as shared /// - shared: indicates if the content should be flagged as shared
@MainActor func _syncAddOrUpdate(_ updateArrays: [SyncedStorableArray], shared: Bool = false) { @MainActor
func _syncAddOrUpdate(_ updateArrays: [SyncedStorableArray], shared: Bool = false) async {
for updateArray in updateArrays { for updateArray in updateArrays {
for item in updateArray.items { for item in updateArray.items {
let storeId: String? = item.getStoreId() let storeId: String? = item.getStoreId()
self.synchronizationAddOrUpdate(item, storeId: storeId, shared: shared) await self.synchronizationAddOrUpdate(item, storeId: storeId, shared: shared)
} }
} }
@ -692,7 +693,7 @@ public class StoreCenter {
/// Processes data that has been revoked /// Processes data that has been revoked
fileprivate func syncRevoke(_ revokedArrays: [ObjectIdentifierArray], parents: [[ObjectIdentifierArray]]) async { fileprivate func syncRevoke(_ revokedArrays: [ObjectIdentifierArray], parents: [[ObjectIdentifierArray]]) async {
self._syncRevoke(revokedArrays) await self._syncRevoke(revokedArrays)
for revokedArray in revokedArrays { for revokedArray in revokedArrays {
for revoked in revokedArray.items { for revoked in revokedArray.items {
await self.synchronizationDelete(id: revoked.modelId, type: revokedArray.type, storeId: revoked.storeId) // or synchronizationRevoke ? await self.synchronizationDelete(id: revoked.modelId, type: revokedArray.type, storeId: revoked.storeId) // or synchronizationRevoke ?
@ -700,17 +701,14 @@ public class StoreCenter {
} }
for level in parents { for level in parents {
self._syncRevoke(level) await self._syncRevoke(level)
} }
} }
fileprivate func _syncRevoke(_ revokeArrays: [ObjectIdentifierArray]) { fileprivate func _syncRevoke(_ revokeArrays: [ObjectIdentifierArray]) async {
for revokeArray in revokeArrays {
Task { for revoked in revokeArray.items {
for revokeArray in revokeArrays { await self.synchronizationRevoke(id: revoked.modelId, type: revokeArray.type, storeId: revoked.storeId)
for revoked in revokeArray.items {
await self.synchronizationRevoke(id: revoked.modelId, type: revokeArray.type, storeId: revoked.storeId)
}
} }
} }
} }
@ -752,12 +750,10 @@ public class StoreCenter {
} }
/// Adds or updates an instance into the store /// Adds or updates an instance into the store
func synchronizationAddOrUpdate<T: SyncedStorable>(_ instance: T, storeId: String?, shared: Bool) { func synchronizationAddOrUpdate<T: SyncedStorable>(_ instance: T, storeId: String?, shared: Bool) async {
let hasAlreadyBeenDeleted: Bool = self._hasAlreadyBeenDeleted(instance) let hasAlreadyBeenDeleted: Bool = self._hasAlreadyBeenDeleted(instance)
if !hasAlreadyBeenDeleted { if !hasAlreadyBeenDeleted {
DispatchQueue.main.async { await self._store(id: storeId).addOrUpdateIfNewer(instance, shared: shared)
self._store(id: storeId).addOrUpdateIfNewer(instance, shared: shared)
}
} }
} }

@ -356,7 +356,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
return false return false
} }
if actionOption.cascade { // when user initiated, we want to cascade delete, but when synchronized, we want the delete notifications to make the job and be sure everything works if actionOption.cascade {
instance.deleteDependencies(store: self.store, actionOption: actionOption) instance.deleteDependencies(store: self.store, actionOption: actionOption)
} }
@ -397,6 +397,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
if let instance = self.findById(realId) { if let instance = self.findById(realId) {
self.deleteItem(instance, actionOption: actionOption) self.deleteItem(instance, actionOption: actionOption)
} }
self.requestWrite()
} }
/// Returns the instance corresponding to the provided [id] /// Returns the instance corresponding to the provided [id]
@ -411,7 +412,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
/// Also removes related API calls /// Also removes related API calls
public func deleteDependencies(_ items: any Sequence<T>) { public func deleteDependencies(_ items: any Sequence<T>) {
defer { defer {
self._triggerWrite = true self.requestWrite()
} }
let itemsArray = Array(items) // fix error if items is self.items let itemsArray = Array(items) // fix error if items is self.items
for item in itemsArray { for item in itemsArray {

Loading…
Cancel
Save