I'm having trouble trying to figure out where I should load in the data from Firebase into my app. I'm not sure where to load the data into viewDidLoad or viewWillAppear. I am currently loading data in viewDidLoad but when I visit another view controller that add items to Firebase to be displayed by the previous view controller. But when I pop back to the original view controller it doesn't show an update in the view controller that supposed to show the item that were updated or added to the database.
The best choice for your case is viewWillAppear as it's being called when you pop back , so make sure to free the array and load it again if it's not a child add observation
viewDidLoad() is only called once after the controller is loaded into memory (more info here: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621495-viewdidload).
If you want to update your data when you go back to your controller, you should do it in viewWillAppear() or viewDidAppear(). The later is called after the former so it's your choice where to call the update function.
In addition, you can also update immediately when there is new data by using NotificationCenter, without the need to go back to your controller.
Related
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!!
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.
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.
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.
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.
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.
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 :
viewWillAppear()
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
I have a view controller. It has some data and values in it. Then it presents modally to another view and moves around and some stuff happens. Then at some point, you self.dismissViewController(). Once back at the original view, can I count on the original data being there? Any help is greatly appreciated. Thanks in advance!
They will not be affected unless you specifically write code to do that for you.
For example, if you call a network request in the viewDidLoad() and add the data to some views, labels, etc. Then you leave that ViewController and come back, nothing will change, i.e., the network request will not be called again.
If you do want to change values in the ViewController every time it appears, use the viewDidAppear() delegate method.
for reloading view on dismiss you must use viewWillAppear() because this working every time before rendering view
calling dismissViewController will remove the viewController. If you do not have a strong reference to the view controller stored elsewhere, dismissing it releases the memory associated with it.
If the presented view controller must return data to the presenting view controller, use the delegation design pattern to facilitate the transfer.
So if the data was not modified in the presented controller then it will be the same then you can depend on it
reference: Apple docs
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!!
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.
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.
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.
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.
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.
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 :
viewWillAppear()
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
I have two view controller A & B.On ViewController A i am displaying data from server.I have nav button Update to go to ViewController B.On B i can update the data which was shown on ViewController B.Now when data is updated on server i show an alert 'Update Success' on click of Ok i go to previous ViewController.But i don't see the updated data because i removed the ViewController B from stack but A is not reloded.
Code for going back to ViewController A
[self.navigationController popViewControllerAnimated:YES];
I assume you are sending a web call from viewDidLoad(). So in order to update your viewController A Add your web call method in viewWillAppear()
The state of your "data" on view controller A has not changed and you need to refresh it. If you're fetching data for view controller A within -viewDidLoad, try moving it to a different life cycle method like -viewWillAppear.
Remember, -viewDidLoad gets called only when the view controller loads which happens once, however -viewWillAppear gets called before it appears which will happen often if you're hopping back and forth between views.
You have to call your API method in viewWillAppear(). So, whenever you come back from any viewController, your updated server data will reload itself.
I'm looking into the viewDidLoad and viewDidAppear methods to better understand what they both do and I came across an article which uses the example of a banking application to explain how these methods work:
Consider a banking application that shows your current balance. A user
can tap on a button, which presents a list of nearby ATMs in a modal
view controller. In order to get the list of nearby ATMs, the
application must make a core location and web service request.
In this situation, a programmer could get away with requesting the
list of nearby ATMs from the server in viewDidLoad. The view
controller is only presented once, both viewDidLoad and
viewWillAppear: will be called back to back and only once for that
particular view controller instance. The net effect of the code in
either of these two methods will be the same.
But this is a bad idea. Consider what would happen if you wanted to
move the ATM view controller into a tab bar controller. Now, the ATM
view controller – with its ATM fetching code in viewDidLoad only
fetches the list of ATMs once. So you are in Atlanta on Tuesday, open
up your application to look for an ATM, then check your balance. Then
you travel to New York on Wednesday, re-open the banking application,
and you only see ATMs in Atlanta. The view was already loaded, no need
to call viewDidLoad and now you’re looking at stale data.
Sadly, I still don't fully understand how/why both viewDidLoad and viewWillAppear will be called 'back to back', or what adding the ATM view controller to a tab bar controller means in terms of these methods.
viewDidLoad method will call only once a life time of viewController and that is when viewController object will first load in memory.
where as viewWillAppear method will call every time when a view will appear to screen or you can say will be topViewController...
Explanation:
Consider you have tab based app with two tabs. Tab1 associated with viewController1 and tab2 is associated with viewController2. Now when you will run your app and you will see tab one is selected and viewController1 is on view and you want to change to tab2, when you will tap on tab2 then tabVieController2's object will create and load to memory first time hence its viewDidLoad method will call, and soon after that it will appear to window and viewWillAppear will also get call.
Now if you you try changing tabs by click on them only viewWillAppear methods will get called for both, as they are in memory already.
It simple, viewDidLoad get called when the view is load in, either via NIB, storyboard or with the loadView method. The viewWillAppear: is called when the view is presented.
When a view is added to a tab bar it only gets load once, thus the viewDidLoad will only be called once. But if the user switch to an other tab and back to the same view the viewDidLoad will not be called. This is because the view is already loaded.
However the viewWillAppear: is called in both cases just before the view is shown. Thus this will be called when the user first opens the tab and when it switches back to that tab.
I think they are referring to the fact that the view is loaded every time the modal view controller appears (thus the data is constantly refreshed) but only once when it is part of tab bar (only loaded on app launch). Kind of a whacky example to explain the methods though.
You might want to read up on the view controller lifecycle to know when to implement what in which method:
Responding to Display-Related Notifications
View Loading and Unloading