Skip to content

exception can remove observer #116

@fukemy

Description

@fukemy

i got this error, please help:

Ảnh chụp Màn hình 2021-04-03 lúc 14 09 09
code:

@objc fileprivate func startLoading() {
        self.updateByContentOffset()
        if self.presentedViewController != nil {
            return
        }
        print("play : \(mmPlayerLayer.playUrl)")
        // start loading video
        mmPlayerLayer.resume()
    }
    
    fileprivate func updateByContentOffset() {
        if let path = findFirstMediaCellVisible(), self.presentedViewController == nil {
            self.updateCell(at: path)
        }
    }
    
    private func findFirstMediaCellVisible() -> IndexPath? {
        let visible = messagesCollectionView.indexPathsForVisibleItems
        guard visible.count > 0 else { return nil }
        
        return visible.first(where: { messagesCollectionView.cellForItem(at: $0) is MediaMessageCell} )
    }
    
    func updateCell(at indexPath: IndexPath) {
        print("updateCell: \(NSStringFromClass(messagesCollectionView.cellForItem(at: indexPath)!.classForCoder))")
        let message = messageList[indexPath.section]
        guard case let .video(videoItem) = message.kind else { return }
        if let cell = messagesCollectionView.cellForItem(at: indexPath) as? MediaMessageCell {
            // this thumb use when transition start and your video dosent start
//            mmPlayerLayer.thumbImageView.image = cell.im
            // set video where to play
            print("set mm for index: \(indexPath) - \(message.sender.displayName)")
            mmPlayerLayer.playView = cell.imageView
            mmPlayerLayer.set(url: videoItem.url)
        }
    }
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        
        offsetObservation = messagesCollectionView.observe(\.contentOffset, options: [.new]) { [weak self] (_, value) in
            guard let self = self, self.presentedViewController == nil else {return}
            NSObject.cancelPreviousPerformRequests(withTarget: self)
            self.perform(#selector(self.startLoading), with: nil, afterDelay: 0.2)
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        NotificationCenter.default.removeObserver(self)
        mmPlayerLayer.player?.pause()
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions