core-data icon indicating copy to clipboard operation
core-data copied to clipboard

Question: Confused with one place in SyncCoordinator

Open superk589 opened this issue 8 years ago • 0 comments

fileprivate func fetchLocallyTrackedObjects() {
        self.perform {
            // TODO: Could optimize this to only execute a single fetch request per entity.
            var objects: Set<NSManagedObject> = []
            for cp in self.changeProcessors {
                guard let entityAndPredicate = cp.entityAndPredicateForLocallyTrackedObjects(in: self) else { continue }
                let request = entityAndPredicate.fetchRequest
                request.returnsObjectsAsFaults = false
                let result = try! self.syncContext.fetch(request)
                objects.formUnion(result)
            }
            self.processChangedLocalObjects(Array(objects))
        }
    }

In SyncCoordinator, since each ChangeProcessor only care about their own tracked objects fetched from cp.entityAndPredicateForLocallyTrackedObjects(in:) Why here union them together and send to every ChangeProcessor

I've changed it to the code below and it works well. So what's the purpose or advantage of the above one?

fileprivate func fetchLocallyTrackedObjects() {
        self.perform {
            // TODO: Could optimize this to only execute a single fetch request per entity.
            for cp in self.changeProcessors {
                guard let entityAndPredicate = cp.entityAndPredicateForLocallyTrackedObjects(in: self) else { continue }
                let request = entityAndPredicate.fetchRequest
                request.returnsObjectsAsFaults = false

                let result = try! self.syncContext.fetch(request)

                self.processChangedLocalObjects(result, using: cp)
            }
        }
    }

superk589 avatar Jul 21 '17 06:07 superk589