Skip to content

Registering change observer without calling block with initial value #44

@JamesBucanek

Description

@JamesBucanek

I have a use pattern where I need to observe changes in a property and trigger special housekeeping and UI related code (like animations).

I do not, however, want to perform these actions for the initial value. As designed, Block-KVO always calls the observation code block with the initial value. I'd like the option of suppressing that behavior.

To that end, I forked the project and added a parallel set of registration methods that take a new observeInitialValue:(BOOL) parameter. So for every method like this

- (void)observeProperty:(NSString *)keyPath withBlock:(MTKBlockChange)observationBlock;

there is now a sister method named

- (void)observeProperty:(NSString *)keyPath observingInitialValue:(BOOL)initial withBlock:(MTKBlockChange)observationBlock;

The implementation is trivial. Each original registration method had the new parameter added. The implementation of the original method is now a single call to its new sister method, passing YES for the observingInitialValue: parameter:

- (void)observeProperty:(NSString *)keyPath withBlock:(MTKBlockChange)observationBlock {
	[self observeProperty:keyPath observingInitialValue:YES withBlock:observationBlock];
}

- (void)observeProperty:(NSString *)keyPath observingInitialValue:(BOOL)initial withBlock:(MTKBlockChange)observationBlock {
    ...
}

The initial call to the change block was removed from -addSettingObservationBlock: and is now the responsibility of the sender—which I think is cleaner architecturally. The initial call is now performed, or skipped, as requested in -observeObject:property:observingInitialValue:withBlock: and -observeRelationship:observingInitialValue:changeBlock:insertionBlock:removalBlock:replacementBlock:.

If this seems like a welcome addition to the project, let me know and I'll make a pull request. If not, just close the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions