Skip to content

Commit

Permalink
refactor: refactor NSManagedObjectID iteration logic
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
ezoushen committed Dec 12, 2023
1 parent 43f2687 commit d1bf81a
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions Sources/DataContainer/UiContextNotifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,25 +367,34 @@ class PersistentHistoryNotificationHandler: NotificationHandler {
// MARK: User Info

struct NSManagedObjectIDIterator: IteratorProtocol {
private var iterators: AnyIterator<AnyIterator<Any>>
private var iterators: AnyIterator<() -> AnyIterator<Any>>
private var iterator: AnyIterator<Any>?

init<I: IteratorProtocol>(_ iterators: I) where I.Element == AnyIterator<Any> {
init<I: IteratorProtocol>(_ iterators: I) where I.Element == () -> AnyIterator<Any> {
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<Any>] = []
var updatedObjectIDIterators: [AnyIterator<Any>] = []
var deletedObjectIDIterators: [AnyIterator<Any>] = []
var insertedObjectIDIterators: [() -> AnyIterator<Any>] = []
var updatedObjectIDIterators: [() -> AnyIterator<Any>] = []
var deletedObjectIDIterators: [() -> AnyIterator<Any>] = []

init(userInfo: [AnyHashable: Any]) {
merge(userInfo: userInfo)
Expand All @@ -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())})
}
}

Expand Down

0 comments on commit d1bf81a

Please sign in to comment.