'AVPlayer values observed via NSKeyValueObservation are always nil

I'm in the process of writing Combine wrappers around AVPlayer. Consider the following example (full source available here):

private final class PlayerRateSubscription<S: Subscriber>: Subscription where S.Input == Float {
    // ...
    private var stateObserverToken: NSKeyValueObservation? = nil
    // ...
    
    func request(_ demand: Subscribers.Demand) {
        // ...
        stateObserverToken = player.observe(\.rate) { [weak self] (player, change) in
            print(change.oldValue)
            print(change.newValue)
            print(player.rate)
            // ...
        }
    }
    // ...
}

When I run that code, change.oldValue and change.newValue both end up being nil. However, player.rate will always return the correct rate.

I'm experiencing the same when observing the playback status: playerItem.status returns the correct value, while the values from the KVO change are always nil. I feel that I should be able to get the old/new values from the change variable.

Am I misusing the API? Is this a known limitation in AVFoundation?

Update

After some research I found the following bits in the documentation:

Omitting the options parameter forgoes storing the new and old property values, which causes the oldValue and newValue properties to be nil.

After updating my implementation to this:

observationToken = observedObject.observe(keyPath, options: [.old, .new]) { [weak self] (object, change) in
    ...
}

some of the observed keypaths started working correctly.

Unfortunately not all of them: according to the docs, AVPlayerItem.status should be key-value observable, yet change.oldValue, and change.newValue are both nil, which I find completely puzzling.



Solution 1:[1]

Just in case someone else is looking for this, this is a bug. https://bugs.swift.org/browse/SR-5872

Update:

bugs.swift was moved to github

Newest issue for this radar ID (SR-5872) since it has had some PRs that have been closed.

https://github.com/apple/swift-corelibs-foundation/issues/3807

Solution 2:[2]

Not all properties are key-value observable by default.

For example in the documentation of status there is

This property is key value observable using Key-value observing.

This line is missing in rate

Solution 3:[3]

The specific location of the image may create that error. I mean the image cant be found. So, try to give the specific location of the image + the image name with its exact extension. ex: "cars.jpg" is different from "cars.jpeg". Also, the image may be invalid or corrupted. try to check the image separately. I tried the code you provided and it worked for me.

import cv2
import numpy as np
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
from numpy.lib.polynomial import poly

img = cv2.imread('/specific location/cars.jpg')
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10, 10))
plt.axis('off')
plt.imshow(img1)
plt.show()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 vadian
Solution 3