From 34055f33332d6da0a7db3d30a8490e949d81e13c Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 25 Feb 2025 17:06:27 +0100 Subject: [PATCH] Fixes an issue where dependencies deletes were not being synced --- LeStorage/ApiCallCollection.swift | 2 +- LeStorage/StoreCenter.swift | 9 +++++++-- LeStorage/StoredCollection+Sync.swift | 6 ++++-- LeStorage/Utils/ClassLoader.swift | 9 +++++---- LeStorage/Utils/Errors.swift | 5 ++++- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/LeStorage/ApiCallCollection.swift b/LeStorage/ApiCallCollection.swift index 657178e..710aea4 100644 --- a/LeStorage/ApiCallCollection.swift +++ b/LeStorage/ApiCallCollection.swift @@ -277,7 +277,7 @@ actor ApiCallCollection: SomeCallCollection { call = try self._createCall(.delete, instance: instance, transactionId: transactionId) default: call = try self._createCall(method, instance: instance, transactionId: transactionId) - StoreCenter.main.log(message: "case \(currentHTTPMethod) / \(method) should not happen") + StoreCenter.main.log(message: "case \(currentHTTPMethod) : \(method) should not happen") } } else { call = try self._createCall(method, instance: instance, transactionId: transactionId) diff --git a/LeStorage/StoreCenter.swift b/LeStorage/StoreCenter.swift index 4618af6..219b38f 100644 --- a/LeStorage/StoreCenter.swift +++ b/LeStorage/StoreCenter.swift @@ -638,11 +638,16 @@ public class StoreCenter { /// Returns a Type object for a class name static func classFromName(_ className: String) throws -> any SyncedStorable.Type { - if let type = ClassLoader.getClass(className) as? any SyncedStorable.Type { - return type + if let type = ClassLoader.getClass(className) { + if let syncedType = type as? any SyncedStorable.Type { + return syncedType + } else { + throw LeStorageError.cantFindClassFromName(name: className) + } } else { throw LeStorageError.cantFindClassFromName(name: className) } + } /// Returns the store corresponding to the provided id, and creates one if necessary diff --git a/LeStorage/StoredCollection+Sync.swift b/LeStorage/StoredCollection+Sync.swift index a730fe7..bd256be 100644 --- a/LeStorage/StoredCollection+Sync.swift +++ b/LeStorage/StoredCollection+Sync.swift @@ -166,6 +166,7 @@ extension StoredCollection: SomeSyncedCollection where T : SyncedStorable { guard sequence.isNotEmpty else { return } for instance in sequence { +// print(">>> SEND DELETE for \(instance.id)") self.deleteItem(instance) StoreCenter.main.createDeleteLog(instance) } @@ -191,8 +192,9 @@ extension StoredCollection: SomeSyncedCollection where T : SyncedStorable { self._sendDeletion(instance) } - public func deleteDependencies(_ items: any Sequence) { - self.delete(contentOfs: items) + public func deleteDependencies(_ items: any RandomAccessCollection) { + guard items.isNotEmpty else { return } + delete(contentOfs: items) // MUST NOT ADD "self" before delete, otherwise it will call the delete method of StoredCollection without sync } // MARK: - Send requests diff --git a/LeStorage/Utils/ClassLoader.swift b/LeStorage/Utils/ClassLoader.swift index 2aa2bf3..439dd85 100644 --- a/LeStorage/Utils/ClassLoader.swift +++ b/LeStorage/Utils/ClassLoader.swift @@ -15,15 +15,16 @@ class ClassLoader { return cachedClass } - if let projectName = Bundle.main.infoDictionary?["CFBundleName"] as? String { - let fullName = "\(projectName).\(className)" + if let bundleName = Bundle.main.infoDictionary?["CFBundleName"] as? String { + let sanitizedBundleName = bundleName.replacingOccurrences(of: " ", with: "_") + let fullName = "\(sanitizedBundleName).\(className)" if let projectClass = _getClass(fullName) { return projectClass } } - let fullName = "LeStorage.\(className)" - if let projectClass = _getClass(fullName) { + let leStorageClassName = "LeStorage.\(className)" + if let projectClass = _getClass(leStorageClassName) { return projectClass } diff --git a/LeStorage/Utils/Errors.swift b/LeStorage/Utils/Errors.swift index 0781a49..866deea 100644 --- a/LeStorage/Utils/Errors.swift +++ b/LeStorage/Utils/Errors.swift @@ -55,8 +55,9 @@ public enum UUIDError: Error, LocalizedError { } } -public enum LeStorageError: Error { +public enum LeStorageError: Error, LocalizedError { case cantFindClassFromName(name: String) + case cantCastTypeToSyncedStorable(name: String) case cantAccessCFBundleName case cantCreateDataAccessBecauseNotInMainStore case cantCreateDataAccessBecauseUserIdIsNil @@ -66,6 +67,8 @@ public enum LeStorageError: Error { switch self { case .cantFindClassFromName(let string): return "can't find class for class name: \(string)" + case .cantCastTypeToSyncedStorable(let string): + return "can't cast type \(string) to SyncedStorable" case .cantAccessCFBundleName: return "can't access CFBundleName for some reason" case .cantCreateDataAccessBecauseNotInMainStore: