线程5:同时访问0x10b883638,但修改需要独占访问

Thread 5: Simultaneous accesses to 0x10b883638, but modification requires exclusive access

我问过 代码并让它工作。

我有 looked here 但那里的答案没有解决我的问题 虽然我解决了这个问题,但我没有在显示的行中收到以下错误。

Thread 5: Simultaneous accesses to 0x10b883638, but modification requires exclusive access

    private var playerItemContext = 0

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    // Only handle observations for the playerItemContext
    print("jdslfhjkfdhaldfahjkflhajfldashkjfdshkjlas")
    guard context == &playerItemContext else {
        super.observeValue(forKeyPath: keyPath, of: object,change: change, context: context)
        return
    }
    ...

为什么会这样,我该如何解决?

试试这个代码:

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

    // Only handle observations for the playerItemContext
    guard context == &P2SheetViewController.playerStatusContext else {
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        return
    }

    if keyPath == #keyPath(AVPlayer.status) {
        let status: AVPlayer.Status
        if let statusNumber = change?[.newKey] as? NSNumber {
            status = AVPlayer.Status(rawValue: statusNumber.intValue)!
        } else {
            status = .unknown
        }

        //Switch over status value
        switch status {
        case .readyToPlay:

            break
        // Player item is ready to play
        case .failed:
            print(".UKNOWN")

            break
        // Player item failed. See error.
        case .unknown:
            print(".UKNOWN")

            break
        // Player item is not yet ready.
        @unknown default: //new jul 17
            print(".UKNOWN")

            break
        }

    }
}

希望对您有所帮助