Dolo
Dolo

Reputation: 976

Will adding multiple methods in shared singleton class cause any performance issue?

In a shared class I created, some methods in the shared class are called only once from a view controller . Do I need to add those method inside view controller class? Will adding those methods directly inside view controller improve performance?

I know singleton instance will be allocated once and it will retain for whole application, In that case calling method from singleton class or adding method directly inside view controller doesn't make any performance difference is what I guess!

Upvotes: 1

Views: 167

Answers (2)

nickbit
nickbit

Reputation: 134

You should keep the methods separate in a Model class (often a singleton) if these methods do not belong in the View Controller class. That way you are implementing the MVC design pattern which is good to apply to your projects for many reasons (read a good design patterns book for more).

Performance is not something you have to worry at this point (you hardly ever see any improvement by adding these methods directly to the View Controller), but code readiness and clean code practices are something that will actually pay off.

Keep also in mind that the view controller is there for mediating control between the model and the views, so anything related to this task should be in the View Controller code.

Upvotes: 2

V-Xtreme
V-Xtreme

Reputation: 7333

Actually the Singleton class instance initialised once per application. So ideally you should use that for sharing common data between view controllers .

Now about method implementation. If those method are doing something which is not related to view controllers for example if those methods are modifying some data and returning back to the controller and if that is common throughout all the controllers then you can implement that in Singleton class . I dont think there will be any big performance issue if you go with either method . Its just matter of clean coding .

Upvotes: 2

Related Questions