Skip to content

Commit 19b6947

Browse files
Fix concurrency warnings
1 parent 11017c8 commit 19b6947

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

Mastodon/In Progress New Layout and Datamodel/Common Components/Views/TimelineRowViews/Molecules/MediaAttachmentView.swift

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ extension MediaAttachment {
764764
}
765765
}
766766

767+
@MainActor
767768
class PlayerObserver: ObservableObject {
768769
@Published var playingState: AVPlayer.TimeControlStatus = .paused
769770
@Published var totalSeconds: Double?
@@ -809,8 +810,10 @@ class PlayerObserver: ObservableObject {
809810
if timeObserverToken == nil {
810811
let interval = CMTime(seconds: 0.5, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
811812
timeObserverToken = player.addPeriodicTimeObserver(forInterval: interval, queue: .main) { [weak self] time in
812-
self?.totalSeconds = player.currentItem?.duration.seconds
813-
self?.currentTimeInSeconds = time.seconds
813+
DispatchQueue.main.async {
814+
self?.totalSeconds = player.currentItem?.duration.seconds
815+
self?.currentTimeInSeconds = time.seconds
816+
}
814817
}
815818
}
816819

@@ -819,11 +822,13 @@ class PlayerObserver: ObservableObject {
819822
object: player.currentItem,
820823
queue: .main
821824
) { [weak self] _ in
822-
if shouldLoop {
823-
self?.player?.seek(to: .zero)
824-
self?.player?.play()
825-
} else {
826-
self?.playShouldSeekToStart = true
825+
DispatchQueue.main.async {
826+
if shouldLoop {
827+
self?.player?.seek(to: .zero)
828+
self?.player?.play()
829+
} else {
830+
self?.playShouldSeekToStart = true
831+
}
827832
}
828833
}
829834
}
@@ -854,9 +859,11 @@ class PlayerObserver: ObservableObject {
854859
let scale = item.duration.timescale
855860
let time = CMTime(seconds: newTime, preferredTimescale: scale)
856861
player?.seek(to: time) { [weak self] finished in
857-
guard let self, let totalSeconds else { return }
858-
self.currentTimeInSeconds = item.currentTime().seconds
859-
self.playShouldSeekToStart = time.seconds >= totalSeconds
862+
DispatchQueue.main.async {
863+
guard let self, let totalSeconds = self.totalSeconds else { return }
864+
self.currentTimeInSeconds = item.currentTime().seconds
865+
self.playShouldSeekToStart = time.seconds >= totalSeconds
866+
}
860867
}
861868
}
862869
}

0 commit comments

Comments
 (0)