UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Pausable workers - what's the Swift Way To Do It?

Forums > Swift

Context: I'm writing a Mac desktop app that allows for online/offline composition and transmission of messages. So, the UI allows the user to compose a message and then say, effectively, "I'm done with this, send it when you can." Then I want to have a background process that monitors network state and attempts to send/receive messages when the network is available.

  • UI - reads messages from the 'incoming' queue and puts them into the application's data store

  • UI - writes messages to the 'outgoing' queue

  • worker - reads messages from the 'outgoing' queue and attempts to write them to a network service

  • worker - reads messages from a network service and writes them to the 'incoming' queue

  • UI - maybe has a button like, "Toggle online/offline" that signals the worker to start trying to do stuff or to pause, as may be

I know how to do all this in Java, using locks and threadpools to manage read/write access to the incoming/outgoing message queues, etc. However, I'm trying to do all this in Swift, so I've been reading up on concurrency and I haven't seen analogous structures. So, what's the Swift way of doing this?

3      

Maybe this article points you in a good direction?

Working with an Internet Connection on iOS with Swift It's dated Aug 2020 so seems recent enough.

Plus there's a section titled Enabling or Disabling App Features Based on Internet Connection which seems to be what you've described above.

Good luck!

Report back, we want to know how you resolved this.

3      

What I think you looking for is to use the Combine API. Mark Moeykens from Big Mountain Studio does a great and clear book on the subject Combine Mastery in SwiftUI. Hope that will help.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.