My title probably makes no sense, but I'll do my absolute best to explain it. So, basically, I have a UITableView that's getting data from Firebase. It has a listener, and any time values are changed on that tree, it updates the tableview. The thing is, I put this in viewDidLoad. (which seemed to make the most sense). Say, if the user goes over to the settings screen (a separate VC), and goes back, it reloads the tableview all over again. A couple users are complaining that it takes long to load the tableview when they come back to the main VC, and I was curious if I could keep the data there on THAT vc, so it's permanent until my listener detects a change in the database. Not sure if that makes any sense - but basically the only time I want the tableview to load data is the initial load, AND when data is changed on my backend. Not every time viewDidLoad gets called.
TL;DR:
How do I make it so tableview loads data once, and the data stays even when switching view controllers
viewDidLoad is only called once during the lifecycle of a viewController. You are using a segue to return to your tableViewController from your settings viewController. A segue always instantiates a new destinationViewController. The only exception is the special unwind segue which returns to a previously instantiated viewController.
So, use an unwind segue to return to your tableViewController and your data will still be there and viewDidLoad will not be called.
Related
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
In my app, there are tabs. I have one tableviewcontroller that contains messages and I have implemented pull to refresh so that works fine. However, if the user goes from the message tab to another tab and then back to the message tab, the uitableview doesn't reload and the user has to pull to refresh. I have thought of putting [self.tableview reloadData] or [self loadObjects] (i am using Parse) in viewDidLoad/viewWilAppear, but that doesn't seem to work...it's because they are only called when the view controller is initially visited right? So I'm wondering as to where I should put that code so that the table view can be reloaded every time the view controller is revisited?
viewWillAppear and viewDidAppear are both called every time the view controller appears. If you call reloadData in one of those methods then it will refresh the table view.
I think your problem is you aren't updating your data source. You will need to make another call to Parse otherwise your table view will just reload with the same data.
I don't think that this behavior is desirable. I suggest you tu wait for a reasonable timeout before updating data, or your user will experience lags and high network usage.
However to do that, you should set a delegate that fires when the corresponding view is loaded (take a look here how to get the event that switch tab menu on iphone ) and then call
[yourTableView reloadData]
inside didSelectViewController
I'm using SWRevealViewController in my app (to get the slide out side panel) however whenever the user navigates to another viewController like 'settings' and comes back, everything gets reset. I understand this is normal behaviour for storyboards since a new VC is instantiated and viewDidLoad is called each time. I tried to get around this by storing the VC in an array in the AppDelegate and then going back to the original viewController, this prevented viewDidLoad being called when the original VC is initially re-presented but I still found it get's called randomly when moving between veiwcontroller's, resetting all my properties etc.. On researching, the Apple documentation does say not to assume viewDidLoad will only be called once.
Is this behaviour apparent in UITabBarController when switching tabs as I'm thinking of ditching the SWRevealViewController and using that instead if it's going to be less headache.
Should I be handling this differently, ie. storing the 'state' in NSUserDefaults and restoring on viewDidLoad?
Thanks in advance.
viewDidLoad is called exactly once, when the UIViewController is first loaded into memory. This is where you want to instantiate any instance variables and build any UIViews that live for the entire lifecycle of this UIViewController.
In UITabBarController also the viewdidLoad for UIViewController is called once, when you are switching tabs.
viewDidLoad() method is called only once. Its an integral part of the cycle.
It is called then the respective UIViewController class is loaded into memory.
And yes, if you want to initialise any properties or access and modify the NSUserDefaults, it can and should be done in the viewDidLoad method.
As for your app, whenever the user will switch between different UIViewControllers, the viewDidLoad method will be called for every destination UIViewController.
Also, as correctly pointed out, it'll also be called in the case of a memory warning.
When is the best time to call [reloadData] on a UITableView? viewDidAppear or viewWillAppear? If a view is unloaded, will the underlying tableview be unloaded as well?
The reason I am asking is due to some behavior I am seeing. Let's say a view is asked to reload its datasource, but is deallocated before it can finish. A scenario would be with a UITabBarController and navigating to a different view than the ViewController being selected. So what happens is that viewWillAppear gets called, but viewDidAppear does not (since I navigated away).
As a result, the ViewController gets deallocated (along with its model data), but if I am calling reloadData in viewWillAppear, the "cellForRow", and "numberOfRows" methods are invoked which causes a "deallocated instance" error. Does that make sense? Do you normally put in code that checks for nil if the model can be changed anytime the view is requested to appear?
I would load the table once the view has loaded, it allows for all of the UIObjects to be created before messing with them. Not sure, but I believe the table is automatically unloaded with the superview.
Take a look at this question>>>
Objective C - Correct way to empty and reload UITableViewController with NSMutableArray
before you get the datasource from server or anywhere, you can display the tableview with a blankcell(a cell that you can draw sth like 404 web page) , when you get the data, then reload the tableview.
remember to set number of cell to 1 for the blankcell
I have a simple iPhone app based on a navigation controller. There are two view controllers, VC1 and VC2, both with table views. VC2 also has a custom table cell. When a user selects a row in VC1, VC2 is pushed on to the stack. When the user selects the back button it's removed. Typical navigation stuff.
The problem I have is that the data in the cells in VC2 persists when the back button is pressed, so that when the user selects a different row in VC1, VC2 is pushed back on to the stack with the 'old' data in the cells, before the methods in VC2 reload the data.
I want to make sure that the data in the table in VC is removed every time the back button is pressed. I've tried releasing the tableview using viewWillDisappear, but it's not working. What's the recommended way of dealing with this situation? I've looked at the docs but it's not obvious (to me at least).
Try out this code snippet in viewWillDissapear or dealloc method.
if(yourTableViewCellObject) [yourTableViewCellObject release];
if(yourTableViewCellObject) yourTableViewCellObject=nil;
This might work.
I've used this technique several times since I asked the original question. As I mentioned in my comment to #Aditya, I've found that the easiest way to deal with this is to use viewWillDisappear to hide the tableview with the 'old' data, and when the user navigates back to the page, wait until the 'new' data is loaded into the table before making the tableview visible again.