Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 895
Kirti Avatar asked Nov 09 '15 10:11

Kirti


People also ask

What is the difference between viewDidLoad and viewDidAppear which should you use to load data from a remote server to display in the view?

The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.

What comes first viewDidLoad or viewWillAppear?

What you can count on: viewDidLoad will be called AT LEAST ONCE when the view is first created and POSSIBLY more times when the view reappears after being hidden. viewWillAppear will ALWAYS be called when the view is about to appear onscreen.

What is difference between viewWillAppear and viewDidAppear?

The viewWillAppear method is called before loading the actual view. The viewDidAppear method is called when the view is already loaded, and you want to show something.

What is the purpose of viewDidLoad in Swift?

Use viewDidLoad( ), which is called AFTER loadView( ) has completed its job and the UIView is ready to be displayed. viewDidLoad( ) allows you to initialize properties of the view/viewController object and finalize them before viewWillAppear( ) is called.


2 Answers

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.

like image 180
JAY RAPARKA Avatar answered Nov 09 '22 23:11

JAY RAPARKA


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.

like image 34
Jamil Avatar answered Nov 09 '22 23:11

Jamil