New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-5872] newValue and oldValue always nil when observing AVPlayerItem.status #3807
Comments
cc @Catfish-Man |
Comment by Christopher Fuller (JIRA) I just tested this in Xcode 9.2 and experience the same issue. This is the test code I used:
Output:
|
Comment by Christopher Fuller (JIRA) This next test utilizes a custom enumeration, instead of AVPlayerItem status, but still nil 🙁
Output:
|
@swift-ci create |
Comment by Holger Wiedemann (JIRA) I can't believe this bug still exists in the Swift 4.1 Core delivered with Xcode 9.3 (9E145). It's pretty obvious why enums don't work with the block based KVO method if you look at the Foundation code let notification = NSKeyValueObservedChange(kind: change.kind,
newValue: change.newValue as? Value,
oldValue: change.oldValue as? Value, Trying to cast a The worst thing about this is that the cast from the change dictionary entry silently fails without any warnings or errors. The value simply becomes nil which may cause all sorts of undefined behavior if you don't know about this bug. |
cc @Catfish-Man, from wiedem (JIRA User)'s description it sounds like this is hopefully a small fix in the overlay? |
Seems very plausible. The main issue is just that I’ve been heads-down on iOS performance work and haven’t had time for overlay stuff. If someone wants to grab this, please feel free, otherwise I’ll see if I can carve out some time. |
It should be fairly easy to fix this for the case where |
Ok I managed to fix it in PMKVObserver for the double-optional case by use of a helper protocol. I feel like there should be a better way but if there is, I'm not sure what. |
I've just opened apple/swift#20757 for this. |
Comment by Christopher Fuller (JIRA) Thanks @ffried for working on this! |
Comment by Christopher Fuller (JIRA) I see there are some comments on @ffried's PR but it is not merged yet. I originally commented on this issue more than a year ago when I first encountered the bug. Any chance we can get attention on this to get it resolved? I would be so very appreciative! |
Comment by Mike Ciesielka (JIRA) This report has gone fairly stale, but I can still reproduce this in Xcode 11.5. As the post mentions, this makes it particularly troublesome to use the block-based KVO apis with AVPlayer. |
maciesielka (JIRA User) Yea, so has the PR. I've kept it up to date (and it's still conflict-free), but since it adds new public APIs (even if those are just overloads), it has to go through some kind of internal review... And apparently that's where this has gone stale. |
Comment by Mike Ciesielka (JIRA) No problem, thanks for working on this @ffried and for bumping the PR as well! |
Comment by Frederick Kellison-Linn (JIRA) I just encountered this issue and managed to track down this bug. Hope to see it resolved soon! |
Comment by Michael Kaye (JIRA) Just wasted the afternoon updating my code to use the use observe KVO approach to find out this doesn't work. Is there any reason this isn't being fixed? |
Maybe it’s considered a “breaking change” at this point (also able to reproduce in Xcode 13.1)…you might try Combine-based KVO, as it doesn’t seem to have this problem. wuf810 (JIRA User) let cancellable = item.publisher(for: \.status).sink { status in
print(status)
} |
Comment by Michael Kaye (JIRA) Thanks @pwightman I'll look into that. |
Thanks @pwightman! I've been meaning to have a look at how Combine solves this and found they worked around this issue in Combine by always getting the current value in the observation callback. |
Comment by Michael Kaye (JIRA) Parker Wightman Thanks for the suggestion to use Combine. Working brilliantly for me. Anyone else coming here looking for a solution, then combine is it. Some example code: let cancellable = player.currentItem?.publisher(for: \.status).sink { status in
print("AVPlayer Status Change: \(status)")
}
let cancellable = player.publisher(for: \.timeControlStatus).sink { timeControlStatus in
print("AVPlayer TimeControlStatus Change: \(timeControlStatus)")
}
|
Attachment: Download
Environment
Xcode 9 beta 6
Additional Detail from JIRA
md5: 6608d02d7d6a7a0859380d1221f9fab8
relates to:
Issue Description:
Using the new observe KVO stuff in Swift 4 doesn't seem to be working for `AVPlayerItem.status`, as `change.newValue` and `change.oldValue` are always nil (note: the `status` property is not an optional).
I noticed that observing this value using the old `addObserver` calls does yield values for `change?[.newKey]` and `change?[.oldKey]`, but they are not `AVPlayerItemStatus` values, they are integers. Which makes sense. But that makes me wonder if perhaps internally, the new KVO system is trying to cast the integer as an `AVPlayerItemStatus`? If so, it seems this would be a generic problem to all enums of a similar nature?
See attached playground (it just contains the above code).
The text was updated successfully, but these errors were encountered: