What is better place for loading data from API- vi

2020-02-09 11:24发布

I am making an IOS app where i am calling an API in viewDidLoad method of view controller. Now i want to reload the same view controller with the data that comes from server. How many ways are there to do this task and what would be the best way?? Please help me.

Thanks!!

6条回答
Evening l夕情丶
2楼-- · 2020-02-09 11:56

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.

查看更多
冷血范
3楼-- · 2020-02-09 11:59

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.

查看更多
对你真心纯属浪费
4楼-- · 2020-02-09 12:05

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:

fruits and cars will come from **API**

suppose Fruits and Cars will present from API.

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

enter image description here

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

查看更多
爷的心禁止访问
5楼-- · 2020-02-09 12:12

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.

查看更多
闹够了就滚
6楼-- · 2020-02-09 12:17

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.

查看更多
家丑人穷心不美
7楼-- · 2020-02-09 12:17

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.

查看更多
登录 后发表回答