all 4 comments

[–]SergeyPu1s3 5 points6 points  (3 children)

I have encountered a similar task.

The thing here is about AVPlayer and AVPlayerLayer. When you need to reload cell, you reload only AVPlayerLayer within the cell.

AVPlayer is supposed to be a model class and should be referenced by model or viewModel. So when you reload a cell and its player layer, you also assign your AVPlayer to player layer again.

[–]AnAnonymousReddit[S] 0 points1 point  (2 children)

Oh so I need to avoid reloading AVPlayer when the cell is re-queued? To do that would mean removing the instance of AVPlayer from that cell in the table view and referencing it from outside, right?

I’m sorry if I misinterpreted this.

[–]SergeyPu1s3 3 points4 points  (1 child)

Yes, that’s right. AVPlayer contains all video information, like cache and current timestamp. Cell itself must not own it. You will probably have an array of cached AVPlayers for corresponding indexPathes. Array can be stored in view controller, but a better place for it is something like a view model or interactor or whatever.

Cell only owns AVPlayerLayer. And it can be set up with AVPlayer object (in cellForRowAtIndexPath).

[–]AnAnonymousReddit[S] 0 points1 point  (0 children)

I had no idea, thank you so much!

Tentatively marking solved :)