Fixes an issue where dependencies deletes were not being synced

sync2
Laurent 9 months ago
parent 61db14f003
commit 34055f3333
  1. 2
      LeStorage/ApiCallCollection.swift
  2. 9
      LeStorage/StoreCenter.swift
  3. 6
      LeStorage/StoredCollection+Sync.swift
  4. 9
      LeStorage/Utils/ClassLoader.swift
  5. 5
      LeStorage/Utils/Errors.swift

@ -277,7 +277,7 @@ actor ApiCallCollection<T: SyncedStorable>: 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)

@ -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

@ -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<T>) {
self.delete(contentOfs: items)
public func deleteDependencies(_ items: any RandomAccessCollection<T>) {
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

@ -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
}

@ -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:

Loading…
Cancel
Save