# `GSKeyValueObservation replaceObservation:observe:forKeyPath:withObserver:options:context:`

*Type Method*

Replaces an existing observation with a new observation.

## Declaration

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

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

```python
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 [`GSKeyValueObservation observe:forKeyPath:withObserver:options:context:`](/Core/GlyphsCore/GSKeyValueObservation/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 [`GSKeyValueObservation observe:forKeyPath:withObserver:options:context:`](/Core/GlyphsCore/GSKeyValueObservation/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.

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

