NEW: Learn to build the incredible iOS 15 Weather app today! >>

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?

   

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.

   

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.

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Learn More

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

Reply to this topic…

You need to create an account or log in to reply.

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.