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

后端 未结 6 1502
无人及你
无人及你 2020-12-16 02:03

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

6条回答
  •  感情败类
    2020-12-16 02:22

    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

提交回复
热议问题