DispatchWorkItem

  Рет қаралды 2,469

AppleProgramming

AppleProgramming

Күн бұрын

Пікірлер: 5
@DeniseNepraunig
@DeniseNepraunig 10 ай бұрын
Really helpful video, thanks a lot!
@strzempa
@strzempa 3 жыл бұрын
Nice, it should be called "how to implement debounce without rx" ;)
@Bazzeee
@Bazzeee 3 жыл бұрын
How does it work when I have a long running process in the work item and I want it to stop its work whenever it is being cancelled? The only way that process can check for itself if it is cancelled is by keeping a reference to its work item object from self. But as you show in the search example, that work item is being replaced by a new instance.
@AppleProgramming
@AppleProgramming 3 жыл бұрын
It is so tricky to do correctly and full of issues that I wouldn't recommend even doing it. Use (NS)Operation or some Combine (if your targets support it) primitive that would work better than this. You'd need something like the following: var request: DispatchWorkItem? request = DispatchWorkItem { print("First step") sleep(2) // work if request?.isCancelled == false { print("Second step") sleep(2) // work } request = nil } searchWorkItem = request let queue = DispatchQueue.global() queue.asyncAfter(deadline: .now() + 0.25, execute: request!) queue.asyncAfter(deadline: .now() + 5) { request?.cancel() request = nil } You need to nil out the reference to request in one way or another. You need an explicit removal outside of just the block itself because if the work gets cancelled then the block is never called and you hold onto the request object. I'll probably be doing a Combine series a little after WWDC so I'll show easier and better solutions for this kind of scenario there. Thanks for watching!
@Bazzeee
@Bazzeee 3 жыл бұрын
@@AppleProgramming Thanks for the thorough reply. I will have a look at Operation and Combine. Love your videos!
Embed Command Line Tool in Mac App
24:29
AppleProgramming
Рет қаралды 5 М.
Advanced Swift L3 - Hashable
20:52
AppleProgramming
Рет қаралды 3,6 М.
Win This Dodgeball Game or DIE…
00:36
Alan Chikin Chow
Рет қаралды 33 МЛН
Самое неинтересное видео
00:32
Miracle
Рет қаралды 2,9 МЛН
Brawl Stars Edit😈📕
00:15
Kan Andrey
Рет қаралды 53 МЛН
Dispatch barrier in Swift || Race Condition  || iOS interview Questions
13:56
Built It - Welcome Window 4
32:48
AppleProgramming
Рет қаралды 1,5 М.
Build It - Welcome Window 1
19:00
AppleProgramming
Рет қаралды 3,6 М.
Launch App from your Command Line Utility
15:28
AppleProgramming
Рет қаралды 2,3 М.
Indirect enums в Swift
9:47
SwiftBook
Рет қаралды 1,9 М.
Win This Dodgeball Game or DIE…
00:36
Alan Chikin Chow
Рет қаралды 33 МЛН