Group Group Group Group Group Group Group Group Group

Implement The Singleton Pattern for Persistency managers Swift

am creating an iOS app and I want to implement Singleton Pattern. I have created “Singleton” LibraryAPI to act as an entry point UserManager object to get data from web API. after that I used a facade Pattern “method” to call UserManager implementation.

https://www.raywenderlich.com/477-design-patterns-on-ios-using-swift-part-1-2#toc-anchor-004

final class LibraryAPI {

    static let shared  =  LibraryAPI()
    private let userManager  =  UsersManager()
    private let isOnline  =  false 

    private init(){

    }

    func getUsers() -> [User] {
        return userManager.getUsers()
    }
}

my questions:

  1. if I have another Manager Class like “album” class should I use the same LibraryAPI and it will become a monolithic class and how to avoid that?
  2. should I create a “Singleton” LibraryAPI class for each manager object like UserLibraryAPI and albumLibraryAPI?

Note: any references or articles are welcome :slight_smile:

How is your data being fetched from the web? This is typically an async operation, if you are reaching back to any of the items in your singleton, you could find yourself in a race condition.

I am using REST web API t get data. In the article they use it. But I want to ask should I create a singleton class for each manager and do facade pattern

I think that depends of who is going to use it. If you are creating a framework for others to use it, then the facade pattern might be a good choice. If only you will be dealing with the app, that’s only a matter of choice.