1

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

John Leonardo
  • 598
  • 3
  • 23
  • `viewDidLoad` only gets called once in the viewController lifecycle. How are you returning to this viewController from the settings VC? – vacawama Sep 18 '16 at 19:06
  • @vacawama segue, nothing fancy, just a performSegue with the main VC identifier. Ugh, I do realize this would have been much better with a nav controller, but that's out of the question now :/ – John Leonardo Sep 18 '16 at 19:08
  • 1
    You shouldn't use a standard segue to "return" to your tableViewController. A segue creates a new copy of the viewController instead of returning to the previous one that is still in memory. You need to use an *unwind segue* to return from your settings view controller. – vacawama Sep 18 '16 at 19:10
  • @vacawama okay, THIS answer could be the one. So just to get this straight, if I use an unwind segue, it won't call viewDidLoad from that initial VC? – John Leonardo Sep 18 '16 at 19:12
  • Correct, an unwind segue will just return to the viewController that is already in memory. It will call `viewWillAppear`, but `viewDidLoad` will not be called again. – vacawama Sep 18 '16 at 19:13
  • @vacawama thanks so much. Can't believe I didn't know about this. Go and post that as an answer and I'll select it – John Leonardo Sep 18 '16 at 19:15

1 Answers1

3

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.

Graham
  • 7,431
  • 18
  • 59
  • 84
vacawama
  • 150,663
  • 30
  • 266
  • 294