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

6

12

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!!

Loathly answered 9/11, 2015 at 10:28 Comment(1)
please accept any answer you found and helped you. so other users can get help if they confused and also they found easily well described answer from multiple answers.Ladonnalady
L
33

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.

Ladonnalady answered 9/11, 2015 at 10:38 Comment(8)
...There is a doubt- viewdidload is called first time a viewcontroller is loaded,but i am calling api in viewcontroller1 and going to viewcontroller2 but when again i am coming back to viewcontroller1 API is calling again!!! Why so??????Loathly
@KirtiKumari R your api calling in viewDidLoad method or viewWillAppear method? Kindly read the 2nd paragraph viewWillAppearLadonnalady
@KirtiKumari i think your api call is in viewWillAppear method. so if you don't want to call again on coming back than you have to call it in viewDidLoad just calm down and read above answer. hope it helps you.Ladonnalady
great explanation.Nat
@DerekSaunders Thank you!Ladonnalady
good explanation, fixed a bug that I was asked to fix :-) thanks !Counterespionage
Thank you @Counterespionage if you found it really helpful then please vote up it.Ladonnalady
Thank you @JAYRAPARKA it is very helpful you save my day..! I accept this answerRoomful
M
2

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.

Malan answered 9/11, 2015 at 10:38 Comment(4)
@Jamil...thanks..I got the concept of viewDidLoad but i am still confused between viewDidappear and viewWillAppear because UI will also stuck until the data loading finish.Loathly
after load the view of viewController this "viewDidAppear" method called. This is the reason it's name is "viewDidAppear". That means this view loaded some time ago (i.e some micro second ago).Malan
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).Malan
If call web service in viewWillAppear() it will get called even in the device rotation as well right? Is this a good practice?Blinking
K
0

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.

Karlakarlan answered 9/11, 2015 at 10:38 Comment(0)
B
0

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.

Bavardage answered 9/11, 2015 at 10:39 Comment(0)
C
0

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.

Carven answered 9/11, 2015 at 10:40 Comment(0)
L
-1

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

Lexi answered 27/6, 2018 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.