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

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

@ -62,8 +62,8 @@ public class StoreCenter {
/// The URL manager
fileprivate var _urlManager: URLManager? = nil
/// Used for testing, gives the project name to retrieve classes from names
var classProject: String? = nil
/// Gives the project name to retrieve classes from names
public var classProject: String? = nil
var useWebsockets: Bool = false
var useSynchronization: Bool = false
@ -640,14 +640,14 @@ public class StoreCenter {
/// Processes the data coming from a sync request
@MainActor func synchronizeContent(_ syncData: SyncData) async {
self._syncAddOrUpdate(syncData.updates)
await self._syncAddOrUpdate(syncData.updates)
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)
// self._syncAddOrUpdate(syncData.relationshipSets)
// await self._syncDelete(syncData.relationshipRemovals)
self._syncAddOrUpdate(syncData.sharedRelationshipSets, shared: true)
self._syncRevoke(syncData.sharedRelationshipRemovals)
await self._syncAddOrUpdate(syncData.sharedRelationshipSets, shared: true)
await self._syncRevoke(syncData.sharedRelationshipRemovals)
Logger.log("sync content: updates = \(syncData.updates.count) / deletions = \(syncData.deletions.count), grants = \(syncData.grants.count)")
@ -667,12 +667,13 @@ public class StoreCenter {
/// - Parameters:
/// - updateArrays: the server updates
/// - 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 item in updateArray.items {
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
fileprivate func syncRevoke(_ revokedArrays: [ObjectIdentifierArray], parents: [[ObjectIdentifierArray]]) async {
self._syncRevoke(revokedArrays)
await self._syncRevoke(revokedArrays)
for revokedArray in revokedArrays {
for revoked in revokedArray.items {
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 {
self._syncRevoke(level)
await self._syncRevoke(level)
}
}
fileprivate func _syncRevoke(_ revokeArrays: [ObjectIdentifierArray]) {
Task {
for revokeArray in revokeArrays {
for revoked in revokeArray.items {
await self.synchronizationRevoke(id: revoked.modelId, type: revokeArray.type, storeId: revoked.storeId)
}
fileprivate func _syncRevoke(_ revokeArrays: [ObjectIdentifierArray]) async {
for revokeArray in revokeArrays {
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
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)
if !hasAlreadyBeenDeleted {
DispatchQueue.main.async {
self._store(id: storeId).addOrUpdateIfNewer(instance, shared: shared)
}
await self._store(id: storeId).addOrUpdateIfNewer(instance, shared: shared)
}
}

@ -356,7 +356,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
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)
}
@ -397,6 +397,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
if let instance = self.findById(realId) {
self.deleteItem(instance, actionOption: actionOption)
}
self.requestWrite()
}
/// Returns the instance corresponding to the provided [id]
@ -411,7 +412,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
/// Also removes related API calls
public func deleteDependencies(_ items: any Sequence<T>) {
defer {
self._triggerWrite = true
self.requestWrite()
}
let itemsArray = Array(items) // fix error if items is self.items
for item in itemsArray {

Loading…
Cancel
Save