Skip to content

Instantly share code, notes, and snippets.

@hhanesand
Created October 27, 2017 18:19
Show Gist options
  • Save hhanesand/50eb99d56c5dc4694b84a774e06a2c59 to your computer and use it in GitHub Desktop.
Save hhanesand/50eb99d56c5dc4694b84a774e06a2c59 to your computer and use it in GitHub Desktop.
- (RLMNotificationToken *)startCreatingTasksForOperationType:(MUSOperationType)type {
RLMResults *results = self.resultsBlock(type);
if (!results) {
return nil;
}
return [results addNotificationBlock:^(RLMResults * _Nullable results, RLMCollectionChange * _Nullable change, NSError * _Nullable error) {
if (error) {
NSLog(@"%@ results error : %@", self, error);
return;
}
if (!change) {
[self processObjects:(id<MUSSubscriptable>)results forOperationType:type];
} else {
// Realm converts moves to deletion and insertion pairs : https://github.com/realm/realm-cocoa/issues/3571
// We only want added or modified objects, so we ignore any moved or deleted objects. (moved == delete/insert pair)
NSMutableSet<NSNumber *> *insertionsWithoutMoves = [NSMutableSet setWithArray:change.insertions];
[insertionsWithoutMoves minusSet:[NSSet setWithArray:change.deletions]];
NSSet *addedOrModified = [insertionsWithoutMoves setByAddingObjectsFromArray:change.modifications];
id<MUSSubscriptable> addedOrModifiedObjects = (id<MUSSubscriptable>)[results mus_objectsAtIndexes:addedOrModified.allObjects];
[self processObjects:addedOrModifiedObjects forOperationType:type];
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment