2

I have a class ServiceClass, a closure function in this class eg LoginViaEmail, calls APILayer. APILayer calls HttpLayer and JSONHandler and returns output to ServiceClass. ServiceClass further returns some info to ViewController when completionblock is received.

My questions are

  1. Shall I make ServiceClass as Singleton? But then everybody says that they are anti-pattern and not a good approach.
  2. If they are so bad, why does Apple use them? eg NSFileManager, NSWorkspace, UIApplication etc,
  3. If not Singleton, then what shall I use? Make instance of ServiceClass every time I use it? Wouldn't that take too much memory?
jscs
  • 62,161
  • 12
  • 145
  • 186
Ella
  • 105
  • 7
  • Please visit following link, https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007072-CH7-SW24 – Parag Ghadge Jun 21 '17 at 08:55
  • Singleton class is not bad option because it is easy to implement and access.you can follow https://www.raywenderlich.com/86477/introducing-ios-design-patterns-in-swift-part-1 – Shabbir Ahmad Jun 21 '17 at 09:08

3 Answers3

0

Don't use a bunch of singletons if you want to write effective, independent and uncoupled, tests. If you don't care about tests, then singletons are fine.

Here's a half-way-house architecture that I use:

  1. Have an overall App or Container class that owns all the services, and creates them, and injects them into each other as needed (so your services have all dependencies as properties, and don't access any statics)

  2. Have your UIAppDelegate class own the single instance of the Container. This is the only 'singleton' in the app.

  3. UIViewControllers etc get references to the Container, and its services through the UIAppDelegate, at viewDidLoad time, and save them into local properties. Eg they inject themselves. It would be nice to make this automatic.

  4. Tests can set up and tear down the Container, or individual services as needed, knowing that other tests can't effect them.

Jon N
  • 1,191
  • 14
  • 26
-1

If your ServiceClass doesn't need some sort of state inside, i would use class func inside and call it like that:

ServiceClass.fetchSomething { fetchedData in
     self.updateUI(with: fetchedData)
}
Michał Kwiecień
  • 2,234
  • 1
  • 17
  • 20
-1

You can use singleton class for this but if you do so then you should manage any network handler queue like functionality also.

Because network requests should be in asynchronous manner with concurrency support.

At any point of time in application, you may need to perform multiple request at once.

Using singleton pattern, this task will be little bit more complex and will need more management of multiple requests.

So if this is not an issue, then you can easily use singleton pattern.

Otherwise you can create a category of NSObject. This category will initiate the request send it to other request handling, parsing classes and then callBack response to sender.

Using category of NSObject, you will have benefit that you can initiate request from any class (as model, controller of any other helper class).

For example as I have used:

self.startPostRequest(withInput: payload,
                                  requestApi: kApiScheduleList,
                                  httpMethod: kHttpMethodPost,
                                  headerDict: nil,
                                  success: { (response) in

            }, failure: { (error) in

            })

Hope this will help.

If you need to discuss on any point, then let me know.

Mohd Haider
  • 591
  • 1
  • 3
  • 24