开发者

Getting maximum performance when making views

I'm developing an app based on a TabBarController. I have 2 views that do some similar actions. My question is, should I make 2 different classes or should I use only one class with some "if" statements asking if is a clas开发者_高级运维s is one or the other. I need maximum performance on this. The 2 views load a MKMapView, so I need to know if it is better to load just one object that does the entire thing, or two objects that do similar things.

Thanks!


In Object Orientation it's important to bear in mind the difference between a class and instances of that class.

If you ever find yourself thinking of writing some code in a class that says "What class am I? If I'm class X, do thing A; otherwise do thing B" -- don't! It's a classic problem begging for a nice object oriented solution. There are two common solutions in this kind of situation:

1) Write a single class that at instantiation time gets passed in some vital information that it then uses later. Then another part of your code is making instances of this class configured in the correct way -- e.g. in your problem, two instances of this class get made, each with a different bit of info (map location perhaps?) passed into the init method

2) Write a superclass that has two subclasses that specialise the general bahaviour of the superclass. So most of your logic and code goes in the superclass - suppose it's called MapDisplayViewController - but then you extend this class with two subclasses called (for example) MapDisplayViewControllerA and MapDisplayViewControllerB that override one or more methods in important, different ways to differentiate them.

For your problem it sounds like approach 1) would be good.

Having code which says "What class am I?" is often a good example of a 'code smell' -- in other words, a sign that something could be designed much better.


I would say load two objects. iOS will automatically unload your currently not displayed views if it needs more resources anyway. (Assuming you implemented viewDidLoad and viewDidUnload properly, of course).

In addition, if in case your view needs to initialize/load a lot of data when tab is switched, and the common flow involves the user switching from one tab to another frequently, the app may appear to lag during frequent loading, if you use only 1 object. No one likes long and frequent loading times.

Just my opinion though, based on the information your original post provides. A lot of additional factors can still come into play.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜