From 64e907156cc5d28be5d53bf71f9c252bb91284ad Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Fri, 3 Jun 2011 00:00:39 -0400 Subject: [PATCH] Minor array controller optimisation: don't send a filterPredicate change notification if clearsFilterPredicateOnInsertion is YES but filterPredicate is already nil when insertObject:atArrangedObjectIndex: is called. --- AppKit/CPArrayController.j | 10 +++++++--- Tests/AppKit/CPArrayControllerTest.j | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/AppKit/CPArrayController.j b/AppKit/CPArrayController.j index 2f72ad429..783c0c3f0 100644 --- a/AppKit/CPArrayController.j +++ b/AppKit/CPArrayController.j @@ -782,8 +782,12 @@ if (![self canAdd]) return; - if (_clearsFilterPredicateOnInsertion) + var willClearPredicate = NO; + if (_clearsFilterPredicateOnInsertion && _filterPredicate) + { [self willChangeValueForKey:@"filterPredicate"]; + willClearPredicate = YES; + } [self willChangeValueForKey:@"content"]; @@ -797,7 +801,7 @@ [_contentObject addObject:anObject]; _disableSetContent = NO; - if (_clearsFilterPredicateOnInsertion) + if (willClearPredicate) [self __setFilterPredicate:nil]; [[self arrangedObjects] insertObject:anObject atIndex:anIndex]; @@ -813,7 +817,7 @@ [self __setSelectionIndexes:[CPIndexSet indexSetWithIndex:0]]; [self didChangeValueForKey:@"content"]; - if (_clearsFilterPredicateOnInsertion) + if (willClearPredicate) [self didChangeValueForKey:@"filterPredicate"]; } diff --git a/Tests/AppKit/CPArrayControllerTest.j b/Tests/AppKit/CPArrayControllerTest.j index aa76c3f5c..439862bbf 100644 --- a/Tests/AppKit/CPArrayControllerTest.j +++ b/Tests/AppKit/CPArrayControllerTest.j @@ -439,6 +439,31 @@ [self assert:1 equals:[observations count] message:@"exactly 1 notification for addObject (clearsFilterPredicate YES)"]; } +/*! + Test that if there is no filter predicate to clear, insertObject:atArrangedObjectIndex: with + clearsFilterPredicate YES does not send a false filterPredicate notification. +*/ +- (void)testObservationDuringInsertObject_atArrangedIndex_ +{ + var arrayController = [self arrayController]; + + [arrayController addObserver:self forKeyPath:@"filterPredicate" options:CPKeyValueObservingOptionOld | CPKeyValueObservingOptionNew context:nil]; + + // Add something to clear. + [arrayController setFilterPredicate:[CPPredicate predicateWithFormat:@"(name != %@)", "Francisco"]]; + observations = []; + var aPerson = [Employee employeeWithName:@"Alexander" department:[Department departmentWithName:@"Cosmic Path Finding"]]; + + [arrayController setClearsFilterPredicateOnInsertion:YES]; + [self assert:0 equals:[observations count] message:@"no observations before insertObject test"]; + [arrayController insertObject:aPerson atArrangedObjectIndex:1]; + [self assert:1 equals:[observations count] message:@"exactly 1 notification for insertObject (clearsFilterPredicate YES)"]; + + // Now that the filter is already cleared, we should not get notified that it clears again on the second insert. + [arrayController insertObject:aPerson atArrangedObjectIndex:1]; + [self assert:1 equals:[observations count] message:@"exactly 1 notification for insertObject x 2 (clearsFilterPredicate YES)"]; +} + - (void)testCompoundKeyPaths { var departmentNameField = [[CPTextField alloc] init];