How to automatically reload the data from viewcontroller? - ios

I am trying to find the solution for how to automatically reload all the data in view controller but i can't find any. Right now i am using push to refresh to reload the button/title/data in view controller viewDidload() but i want to have it automalliy reload everything every time i come back to this viewcontroller. For example when app lunched, it load view controller A then i clicked on the button to go View controller B but i want to controller A to refresh everything after back from B so how can i do that?
Thanks

For this, there is a method viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
reloadData()
}
func reloadData() {
//All you need to update
}

Related

Do not reload content in the top view in a navigation on tab switch

I'm on a view (lets say VC1) which is at the top of navigation stack. I click on another Tab and come back to this view, content in VC1 reloads. How do I stop reloading content in this situation as reloading doesn't make any sense since view is already loaded with content.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
loadContent()
}
You can't stop viewWillAppear to be called from 2 same actions like switching tab or poping a covering vc , so you need to remove viewWillAppear and assign a delegate for the vc you need to reload after it's pop to call loadContent

Avoiding Delay from One View Controller to Another in Xcode using Swift

I have set up a button in my iOS App that performs segue from 1st View Controller to 2nd View Controller. But, there is a delay of about 3-5 seconds on moving from 1st View Controller to 2nd View Controller. Is there any way by which this delay can be avoided and the user can easily go to the 2nd View Controller on the click of the button without any delay? Would appreciate it if anyone could provide any suggestions on how can I eliminate this issue? Thanks a lot for the help:)
#IBAction func startButton(_ sender: Any) {
performSegue(withIdentifier: "1to2segue", sender: self)
}
The 2nd View Controller displays live stats extracted from an external source. Thus, it takes time to load up. How can I make this process faster?
override func viewDidLoad() {
super.viewDidLoad()
loadData()
}
In your second view controller, do NOT call your loadData() func in viewDidLoad().
You want to do as little as needed to get the view on the screen. Show a spinner or some other "Loading Data..." activity view.
Then, perhaps in viewDidAppear(), call your loadData() func. But make sure whatever you're doing in loadData() is being done in an async process.

How do you refresh a table view once dismissing a View Controller after inserting values using SQLite3?

I have a tableViewController that holds a list of activities. Theres a button that takes you to a new view controller, which allows you to create a new activity, that then gets inserted into the database when the save button is pressed, which also dismisses the view controller to take you back to the list of activities. I want the activities to refresh so that the new activity they just inserted will be added to the table. I have tried
nextViewController = ActivitiesViewController()
dismissVC(){
nextViewController.viewDidLoad() }
It currently takes you back to the correct table view, but the rows do not update. It isn't until the phone is restarted that the values update.
Anyone have any ideas on how to fix this?
Thanks
You probably want to override the method viewWillAppear(_ animated: Bool) in your ActivitiesViewController and call a function to update the data (just as you do in viewDidLoad).
class ActivitiesViewController: UITableViewController { // UITableViewController or whatever you're using
// your stuffs
// ...
// ...
override func viewDidLoad() {
super.viewDidLoad()
// load datas
tableView.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// load datas
tableView.reloadData()
}
}
To explain a bit further why you should do this; in your code snippet, you're creating a new instance of ActivitiesViewController that is not the one that is displayed on the screen. Even if you update it, it's not linked to the one that is displayed.

When navigating back in navigation controller view doesn't load

I have weird situation and have no clue how to debug it. I load three viewControllers in navigation controller. When Im navigating back from there second and first ViewController doesn't display anything just white screen I added print methods everywhere in lifecycle methods and it seems that it loads views but anyway they not visible. What could be the problem?
Yep It's weird, There maybe some code which do something with your view on such events like:
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// remove some subviews or change constraints.
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// remove some subviews or change constraints.
}
Please send us a code of the view controller which has the problems and code how exactly you show the controller.

Redundant tableview after fetch request

I have a UITableView and the cells display data that is fetched from my backend. I can navigate to different view controllers from the view controller that contains my tableview. When I segue back to my view controller with the tableview, I want to update the table with an updated fetch request.
As of now I have:
override func viewDidLoad() {
self.fetchData()
self.dataTable.reloadData()
}
override func viewWillAppear(animated: Bool)
{
self.fetchData()
self.dataTable.reloadData()
}
This just causes the tableview to be redundant and display the same data (obviously). What would be the correct way to update the table without displaying the same information? Should I clear the array from the prior fetch request before each fetch request?
You have to remember that viewDidLoad() is only called once during navigation segues. So, after it's loaded, the only time you'll know that the view has popped up again is in the viewWillAppear().
Have you tried:
override func viewWillAppear(animated: Bool)
{
self.clearData()
//Once you've reset all your variables, populate them again.
self.fetchData()
self.audioData.reloadData()
self.dataTable.reloadData()
}
If I'm wrong, and your viewDidLoad() method is being called every time the view appears, then your code should work. You can always put print statements in each function to see what's being called, and what isn't. Let me know if this helps :)

Resources