As I migrate my apps to Swift 6 one by one, I am gaining a deeper understanding of concurrency. In the process, I am quite satisfied to see the performance benefits of parallel programming being integrated into my apps.
At the same time, I have come to think that actor is a great type for addressing the 'data race' issues that can arise when using the 'singleton' pattern with class.
Specifically, by using actor, you no longer need to write code like private let lock = DispatchQueue(label: "com.singleton.lock") to prevent data races that you would normally have to deal with when creating a singleton with a class. It reduces the risk of developer mistakes.
import EventKit
actor EKDataStore: Sendable {
static let shared = EKDataStore()
let eventStore: EKEventStore
private init() {
self.eventStore = EKEventStore()
}
}
Of course, since a singleton is an object used globally, it can become harder to manage dependencies over time. There's also the downside of not being able to inject dependencies, which makes testing more difficult.
I still think the singleton pattern is ideal for objects that need to be maintained throughout the entire lifecycle of the app with only one instance. The EKDataStore example I gave is such an object.
I’d love to hear other iOS developers' opinions, and I would appreciate any advice on whether I might be missing something 🙏
Post
Replies
Boosts
Views
Activity
Hello.
After updating to iOS17 and Xcode 15, the following warning appears in xcode. It worked great before and I didn't change anything. Has anyone solved it?
nw_parameters_set_source_application_by_bundle_id_internal Failed to convert from bundle ID (my bundel ID) to UUID. This could lead to wrong data usage accounting.
Best Regards