5

I'm watching an NSArray property with KVO. I've implemented KVC like in this post and I also implemented most of the KVC array accessors. To mutate it, I use mutableArrayValueForKey. It works fine, except to 2 issues:

  1. When I call removeAllObjects, I get a NSKeyValueChangeRemoval change for each single removed item. I'd like to receive only one NSKeyValueChangeRemoval notification with all removed indexes in it.

  2. Similarly when I call addObjectsFromArray:, I get NSKeyValueChangeInsertion for each single added item. I'd like to receive only one NSKeyValueChangeInsertion notification with all added indexes in it.


Notice that I do have implemented the KVC methods remove<Key>ItemsAtIndexes: and insert<Key>Items:atIndexes:. They are not called though.

I use the following workarounds:

- (void)removeAllObjectsWorkaroundFromArray:(NSMutableArray *)modelArray {
    NSRange indexRange;
    indexRange.length = modelArray.count;
    indexRange.location = 0;
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:indexRange];
    [modelArray removeObjectsAtIndexes:indexSet];
}

- (void)addObjectsFromArrayWorkaroundWithArray:(NSMutableArray *)modelArray arrayToAdd:(NSArray *)arrayToAdd {
    NSRange indexRange;
    indexRange.length = arrayToAdd.count;
    indexRange.location = modelArray.count;
    NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndexesInRange:indexRange];
    [modelArray insertObjects:arrayToAdd atIndexes:indexSet];
}

Is there a way to directly use removeAllObjects and addObjectsFromArray: without the need for the above workarounds?

Community
  • 1
  • 1
fabb
  • 11,660
  • 13
  • 67
  • 111

1 Answers1

0

As I am sure you are aware one cannot observe the array itself, just attributes of it. So I think your workaround is unavoidable.

That being said - I really like the way you solved this!

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Could you elaborate a bit more on why you think it's unavoidable? I guess the problem stems from the implementation of the methods `removeAllObjects` and `addObjectsFromArray:` which add/remove items one-by-one. To be sure I asked the question, maybe I overlooked something. When the workaround is unavoidable, I'd make the workaround methods category-methods on `NSArray`. – fabb Aug 06 '13 at 08:59
  • As I said, the objects of the array are observed, not the array itself. That explains the unavoidable. Category seems a good way to go. – Mundi Aug 06 '13 at 11:15