Declaration

class func replace(_ observation: UnsafeMutablePointer<GSKeyValueObservation?>, observe object: NSObject?, forKeyPath keyPath: String, withObserver observer: NSObject, options: NSKeyValueObservingOptions = [], context: UnsafeMutableRawPointer?)

Declaration

+ (void) replaceObservation:(GSKeyValueObservation **) observation observe:(NSObject *) object forKeyPath:(NSString *) keyPath withObserver:(NSObject *) observer options:(NSKeyValueObservingOptions) options context:(void *) context;

Declaration

replaceObservation_observe_forKeyPath_withObserver_options_context_(object: NSObject, keyPath: str, observer: NSObject, options: NSKeyValueObservingOptions, context) -> GSKeyValueObservation

Parameters

observation

A pointer to the observation to stop and then replace. Can be a pointer to nil when no current observation exists.

observer

When nil, stops the observation.

Discussion

This is a thin wrapper around observe(_:forKeyPath:withObserver:options:context:)observe:forKeyPath:withObserver:options:context:observe_forKeyPath_withObserver_options_context_ that ensures that the new observation does not start before the given observation ended. Simply replacing the existing observation with the new observation, as shown in the example from observe(_:forKeyPath:withObserver:options:context:)observe:forKeyPath:withObserver:options:context:observe_forKeyPath_withObserver_options_context_, works in most cases. However, this creates the new observation just before the existing observation is replaced, leaving a narrow timeframe where both observations are active. Using this method eliminates that timeframe.

- (void)thingDidUpdate:(Thing *)newThing {
    [GSKeyValueObservation replaceObservation:&_someObservation observe:newThing ...];
}