What is better place for loading data from API- viewDidLoad, viewWillAppear or viewDidAppear?

99封情书 提交于 2019-11-29 04:22:21
JAY RAPARKA

viewDidLoad method is called first time when UIViewController is first loaded and when it pop and then you reenter in it at that time viewDidLoad is called. So if you want to load the API only once then viewDidLoad is the best place to call an API.

viewWillAppear called every time when you enter in that UIViewController and it is the place load the API when you want to get refreshed data (updated data).

viewDidAppear also called like viewWillAppear but bit late called than viewWillAppear so if you want to call the API every time than the best place is viewWillAppear method.

Because viewDidAppear method called late from viewWillAppear method and you are just requesting the API so the response of API may be late and If your UI change based on API response then it will stuck the application UI so there is a best place to call API either viewDidLoad & viewWillAppear methods.

viewDidLoad is called once. If you use navigation controller and do back and forth ou view controller this viewDidLoad method will never be called. Until you create this ViewController again (i.e [navContoller pushViewController]). If your api data will never changed the life cycle of this View Controller the this is the better place to call your API. But if your api data need to call frequently [i.e. back and push.forth this view controller] then you should not call api here.

viewWillAppear: before a view controller shows.If you call you api inside this method you UI will stack until the data loading finish. which looks odd.before load the view of viewController this "viewWillAppear" method is called. This is the reason, it's name is "viewWillAppear". That means this view will load some time later (i.e some micro second later). If you call your api here after what will happen lets analyze. Say, your api return response after 10 sec. Then UI will freeze/stuck for 10 sec and you will see after this 10 sec later your view will called.

viewDidAppear: after finished a view controller showing.So, you need to called your loading API inside this method.

viewDidAppear is definitely not the one you want to use, it will 'pause' the view from responding while you're loading the data.

Normally viewDidLoad is the one you want to place it.

If we stay in the same ViewController,the three method viewdidload,viewwillappear,viewdidappear will not called again.So we stay in the same view controller, we get the data from the server,we should call the reload method after get the data. Hope this answer can help you.

I think viewWillAppear is best place for loading data from API. Because viewDidLoad called once when the view is being loaded, but viewWillAppear will called when it loaded from its parent view or child view.

You don't need to call API every time you navigate to view controller, you need to call it one time.

If you have a TableView with Cell and this cell get from API and will open new ViewController when you press on it.

So here you will not add your API in :

  1. viewWillAppear()
  2. viewDidAppear()

You will add it one time in viewDidLoad() while we need to minimize num of requests as many as we can.

Example like this: navigation controller:

suppose Fruits and Cars will present from API.

when you click on the fruits cell you will navigate to below viewController:

So when you want to go back to the first view controller, clearly you dont need to reload api while it is already exist.

In this case we use viewDidLoad() to handle API request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!