Hello, I came from Android world and still feel confused in IOS… The reason is design pattern. For instance one of coolest and actually preferable by Google way of developing android app is using MVVM + repository. Basically, View gets data from ViewModel, which then provides data to repository, which then makes network requests and sends back observable. I tried to something similar in IOS. For instance I have 3 VCS and each of them sends particular data to API and after all three VSC sends data(Files and other heavy stuff) I need to make 4-5 network requests**. I created a Repository(Singleton) and every viewController(ViewModel) creates network request via Repository.shared.createRequest(data and etc).** Then I have dispatchGroup which decides then I have received all responses and fires my necessary requests after that. Repository also creates Delegate, which my 4 VC conforms to. This way my Viecntrollers just get data and open another viewcontroller immediately, but all responses and logic are handled in Repository(Singleton)… Why its is bad design pattern? I don’t find alternatives… I mean**, I could create few delegetes in my 3 VC and conform to those delegates in 4 VC(then I receive response)…**. However in future this pattern does not scale entirely… Please provide actual recomendtations about there should I store data which is common for all VC’S and there should I create Network requests? In viewModel? in Vc? or in Singleton?
Hi @wellbranding, I recently responded in a different post but I will share my same response here as well.
I wouldn’t necessarily that singletons are either good or bad. This is something that I’ve seen other devs blog about but I do believe there could be a smart way of approaching singletons. For example, if we have this singleton
class Recipe {
static let shared = Recipe()
private init() { }
func do(_ message: String) {
print(message)
}
}
Then we can use it as Recipe.shared.log(). Another way to go about this could be,
Hi @wellbranding,
Have you tried looking at Alamofire this allows you to work in a smarter way with network requests.
As far as Design patterns are concerned, each has their advantage and dis-advantage, specially on particular platforms. There are some amazing tutorials and a book on this site on this topic.
My 2 cents worth is that singletons is now less and less recommended, instead Dependency Injection is suggested in its place.