From d1bf81a7ee270c65bc3b9cd44e6d26d7abe98ef6 Mon Sep 17 00:00:00 2001 From: ezoushen Date: Tue, 12 Dec 2023 13:20:25 +0800 Subject: [PATCH] refactor: refactor NSManagedObjectID iteration logic - Refactor iterators in `NSManagedObjectIDIterator` to use a closure that returns `AnyIterator` instead of a direct `AnyIterator`. - Adjust `next()` method in `NSManagedObjectIDIterator` to handle the closure-based iterator and to fall back to the next iterator if the current one is exhausted. - Update `UserInfoMerger` to store arrays of closures returning `AnyIterator`s for inserted, updated, and deleted `NSManagedObjectID`s. - Modify `UserInfoMerger` initializer to wrap `makeIterator()` calls in closures, aligning with changes to iterator storage. Signed-off-by: ezoushen --- Sources/DataContainer/UiContextNotifier.swift | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/Sources/DataContainer/UiContextNotifier.swift b/Sources/DataContainer/UiContextNotifier.swift index 313f4915..d9f6f884 100644 --- a/Sources/DataContainer/UiContextNotifier.swift +++ b/Sources/DataContainer/UiContextNotifier.swift @@ -367,25 +367,34 @@ class PersistentHistoryNotificationHandler: NotificationHandler { // MARK: User Info struct NSManagedObjectIDIterator: IteratorProtocol { - private var iterators: AnyIterator> + private var iterators: AnyIterator<() -> AnyIterator> private var iterator: AnyIterator? - init(_ iterators: I) where I.Element == AnyIterator { + init(_ iterators: I) where I.Element == () -> AnyIterator { self.iterators = AnyIterator(iterators) } mutating func next() -> NSManagedObjectID? { if iterator == nil { - iterator = iterators.next() + iterator = iterators.next()?() + } + if iterator == nil { + return nil + } else { + if let value = iterator?.next() as? NSManagedObjectID { + return value + } else { + iterator = nil + return next() + } } - return iterator?.next() as? NSManagedObjectID } } class UserInfoMerger { - var insertedObjectIDIterators: [AnyIterator] = [] - var updatedObjectIDIterators: [AnyIterator] = [] - var deletedObjectIDIterators: [AnyIterator] = [] + var insertedObjectIDIterators: [() -> AnyIterator] = [] + var updatedObjectIDIterators: [() -> AnyIterator] = [] + var deletedObjectIDIterators: [() -> AnyIterator] = [] init(userInfo: [AnyHashable: Any]) { merge(userInfo: userInfo) @@ -401,25 +410,25 @@ class UserInfoMerger { guard let userInfo = userInfo else { return } if let insertedObjectIDs = userInfo[AnyHashable(NSInsertedObjectIDsKey)] as? NSSet { - insertedObjectIDIterators.append(AnyIterator(insertedObjectIDs.makeIterator())) + insertedObjectIDIterators.append({AnyIterator(insertedObjectIDs.makeIterator())}) } if let insertedObjects = userInfo[AnyHashable(NSInsertedObjectsKey)] as? NSSet { - insertedObjectIDIterators.append(AnyIterator( - insertedObjects.lazy.compactMap{ ($0 as? NSManagedObject)?.objectID }.makeIterator())) + insertedObjectIDIterators.append({AnyIterator( + insertedObjects.lazy.compactMap{ ($0 as? NSManagedObject)?.objectID }.makeIterator())}) } if let updatedObjectIDs = userInfo[AnyHashable(NSUpdatedObjectIDsKey)] as? NSSet { - updatedObjectIDIterators.append(AnyIterator(updatedObjectIDs.makeIterator())) + updatedObjectIDIterators.append({AnyIterator(updatedObjectIDs.makeIterator())}) } if let updatedObjects = userInfo[AnyHashable(NSUpdatedObjectsKey)] as? NSSet { - updatedObjectIDIterators.append(AnyIterator( - updatedObjects.lazy.compactMap{ ($0 as? NSManagedObject)?.objectID }.makeIterator())) + updatedObjectIDIterators.append({AnyIterator( + updatedObjects.lazy.compactMap{ ($0 as? NSManagedObject)?.objectID }.makeIterator())}) } if let deletedObjectIDs = userInfo[AnyHashable(NSDeletedObjectIDsKey)] as? NSSet { - deletedObjectIDIterators.append(AnyIterator(deletedObjectIDs.makeIterator())) + deletedObjectIDIterators.append({AnyIterator(deletedObjectIDs.makeIterator())}) } if let deletdeObjects = userInfo[AnyHashable(NSDeletedObjectsKey)] as? NSSet { - deletedObjectIDIterators.append(AnyIterator( - deletdeObjects.lazy.compactMap{ ($0 as? NSManagedObject)?.objectID }.makeIterator())) + deletedObjectIDIterators.append({AnyIterator( + deletdeObjects.lazy.compactMap{ ($0 as? NSManagedObject)?.objectID }.makeIterator())}) } }