From afea8d5aacf474b57b4364feda08be9ca195594b Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 12 Jun 2018 10:58:35 -0700 Subject: Refactor Remote Event (#1396) --- Firestore/Source/Local/FSTLocalStore.mm | 38 +++++++++++---------------------- 1 file changed, 12 insertions(+), 26 deletions(-) (limited to 'Firestore/Source/Local') diff --git a/Firestore/Source/Local/FSTLocalStore.mm b/Firestore/Source/Local/FSTLocalStore.mm index 7469c71..6aab78e 100644 --- a/Firestore/Source/Local/FSTLocalStore.mm +++ b/Firestore/Source/Local/FSTLocalStore.mm @@ -268,46 +268,32 @@ NS_ASSUME_NONNULL_BEGIN FSTListenSequenceNumber sequenceNumber = [self.listenSequence next]; id queryCache = self.queryCache; - [remoteEvent.targetChanges enumerateKeysAndObjectsUsingBlock:^( - NSNumber *targetIDNumber, FSTTargetChange *change, BOOL *stop) { - FSTTargetID targetID = targetIDNumber.intValue; + for (const auto &entry : remoteEvent.targetChanges) { + FSTTargetID targetID = entry.first; + FSTBoxedTargetID *boxedTargetID = @(targetID); + FSTTargetChange *change = entry.second; // Do not ref/unref unassigned targetIDs - it may lead to leaks. - FSTQueryData *queryData = self.targetIDs[targetIDNumber]; + FSTQueryData *queryData = self.targetIDs[boxedTargetID]; if (!queryData) { - return; + continue; } + [queryCache removeMatchingKeys:change.removedDocuments forTargetID:targetID]; + [queryCache addMatchingKeys:change.addedDocuments forTargetID:targetID]; + // Update the resume token if the change includes one. Don't clear any preexisting value. // Bump the sequence number as well, so that documents being removed now are ordered later // than documents that were previously removed from this target. NSData *resumeToken = change.resumeToken; if (resumeToken.length > 0) { - queryData = [queryData queryDataByReplacingSnapshotVersion:change.snapshotVersion + queryData = [queryData queryDataByReplacingSnapshotVersion:remoteEvent.snapshotVersion resumeToken:resumeToken sequenceNumber:sequenceNumber]; - self.targetIDs[targetIDNumber] = queryData; + self.targetIDs[boxedTargetID] = queryData; [self.queryCache updateQueryData:queryData]; } - - FSTTargetMapping *mapping = change.mapping; - if (mapping) { - // First make sure that all references are deleted. - if ([mapping isKindOfClass:[FSTResetMapping class]]) { - FSTResetMapping *reset = (FSTResetMapping *)mapping; - [queryCache removeMatchingKeysForTargetID:targetID]; - [queryCache addMatchingKeys:reset.documents forTargetID:targetID]; - - } else if ([mapping isKindOfClass:[FSTUpdateMapping class]]) { - FSTUpdateMapping *update = (FSTUpdateMapping *)mapping; - [queryCache removeMatchingKeys:update.removedDocuments forTargetID:targetID]; - [queryCache addMatchingKeys:update.addedDocuments forTargetID:targetID]; - - } else { - HARD_FAIL("Unknown mapping type: %s", mapping); - } - } - }]; + } // TODO(klimt): This could probably be an NSMutableDictionary. DocumentKeySet changedDocKeys; -- cgit v1.2.3