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!!
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.viewDidLoad
method is called first time whenUIViewController
is first loaded and when it pop and then you reenter in it at that timeviewDidLoad
is called. So if you want to load the API only once thenviewDidLoad
is the best place to call an API.viewWillAppear
called every time when you enter in thatUIViewController
and it is the place load the API when you want to get refreshed data (updated data).viewDidAppear
also called likeviewWillAppear
but bit late called thanviewWillAppear
so if you want to call the API every time than the best place isviewWillAppear
method.Because
viewDidAppear
method called late fromviewWillAppear
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 eitherviewDidLoad
&viewWillAppear
methods.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 :
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
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.
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.
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.