Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e1d6829

Browse files
Update README.md
1 parent 9f2e631 commit e1d6829

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎README.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,44 @@ Please note that using videos from URLs requires ensuring that you have the righ
146146

147147
## Commands
148148

149+
### Handling Commands
150+
```swift
151+
@State public var playbackCommand: PlaybackCommand = .idle
152+
```
153+
`@State` updates are asynchronous and batched in SwiftUI. When you assign:
154+
```swift
155+
playbackCommand = .play
156+
playbackCommand = .pause
157+
```
158+
SwiftUI only registers the last assignment (`.pause`) in the same run loop cycle, ignoring `.play`.
159+
To ensure .play is applied before .pause, you can use `Task` to schedule the second update on the next run loop iteration:
160+
**.play → .pause**
161+
```swift
162+
playbackCommand = .play
163+
Task { @MainActor in
164+
playbackCommand = .pause
165+
}
166+
```
167+
**.play .pause .play**
168+
169+
```swift
170+
playbackCommand = .play
171+
172+
Task {
173+
playbackCommand = .pause
174+
Task { playbackCommand = .play } // This runs AFTER `.pause`
175+
}
176+
```
177+
If playbackCommand is critical for playback logic, use an ObservableObject with @Published instead of @State. This avoids SwiftUI’s state batching
178+
```swift
179+
@Published var playbackCommand: PlaybackCommand = .pause
180+
```
181+
then works
182+
```swift
183+
playbackCommand = .play
184+
playbackCommand = .pause
185+
```
186+
149187
### Handling Sequential Similar Commands
150188

151189
When using the video player controls in your SwiftUI application, it's important to understand how command processing works. Specifically, issuing two identical commands consecutively will result in the second command being ignored. This is due to the underlying implementation in SwiftUI that prevents redundant command execution to optimize performance and user experience.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /