Tableview not loading data after popping View Controller - swift

When I pop the view controller stack, I need a table view in the first view controller to reload. I am using viewWillAppear (I already tried viewDidAppear and it didn't work).
override func viewWillAppear(_ animated: Bool) {
print("will appear")
loadData()
}
Once the view controller has re-appeared, I need to query the API again which I am doing in another service class with a completion handler of course and then reloading the table view:
#objc func loadData() {
guard let userEmail = userEmail else { return }
apiRequest(userId: userEmail) { (queriedArticles, error) in
if let error = error {
print("error in API query: \(error)")
} else {
guard let articles = queriedArticles else { return }
self.articlesArray.removeAll()
self.articleTableView.reloadData()
self.articlesArray.append(contentsOf: articles)
DispatchQueue.main.async {
self.articleTableView.reloadData()
}
}
}
}
What happens is that I am able to pop the stack and see the first view controller BUT it has the same data as it did before. I expect there to be one more cell with new data and it doesn't appear. I have to manually refresh (using refresh control) to be able to query and load the new data.
Any idea what I am doing wrong?

Related

Swift launch view only when data received

I'm getting info from an API using the following function where I pass in a string of a word. Sometimes the word doesn't available in the API if it doesn't available I generate a new word and try that one.
The problem is because this is an asynchronous function when I launch the page where the value from the API appears it is sometimes empty because the function is still running in the background trying to generate a word that exists in the API.
How can I make sure the page launches only when the data been received from the api ?
static func wordDefin (word : String, completion: #escaping (_ def: String )->(String)) {
let wordEncoded = word.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let uri = URL(string:"https://dictapi.lexicala.com/search?source=global&language=he&morph=false&text=" + wordEncoded! )
if let unwrappedURL = uri {
var request = URLRequest(url: unwrappedURL);request.addValue("Basic bmV0YXlhbWluOk5ldGF5YW1pbjg5Kg==", forHTTPHeaderField: "Authorization")
let dataTask = URLSession.shared.dataTask(with: request) { (data, response, error) in
do {
if let data = data {
let decoder = JSONDecoder()
let empty = try decoder.decode(Empty.self, from: data)
if (empty.results?.isEmpty)!{
print("oops looks like the word :" + word)
game.wordsList.removeAll(where: { ($0) == game.word })
game.floffWords.removeAll(where: { ($0) == game.word })
helper.newGame()
} else {
let definition = empty.results?[0].senses?[0].definition
_ = completion(definition ?? "test")
return
}
}
}
catch {
print("connection")
print(error)
}
}
dataTask.resume()
}
}
You can't stop a view controller from "launching" itself (except not to push/present/show it at all). Once you push/present/show it, its lifecycle cannot—and should not—be stopped. Therefore, it's your responsibility to load the appropriate UI for the "loading state", which may be a blank view controller with a loading spinner. You can do this however you want, including loading the full UI with .isHidden = true set for all view objects. The idea is to do as much pre-loading of the UI as possible while the database is working in the background so that when the data is ready, you can display the full UI with as little work as possible.
What I'd suggest is after you've loaded the UI in its "loading" configuration, download the data as the final step in your flow and use a completion handler to finish the task:
override func viewDidLoad() {
super.viewDidLoad()
loadData { (result) in
// load full UI
}
}
Your data method may look something like this:
private func loadData(completion: #escaping (_ result: Result) -> Void) {
...
}
EDIT
Consider creating a data manager that operates along the following lines. Because the data manager is a class (a reference type), when you pass it forward to other view controllers, they all point to the same instance of the manager. Therefore, changes that any of the view controllers make to it are seen by the other view controllers. That means when you push a new view controller and it's time to update a label, access it from the data property. And if it's not ready, wait for the data manager to notify the view controller when it is ready.
class GameDataManager {
// stores game properties
// updates game properties
// does all thing game data
var score = 0
var word: String?
}
class MainViewController: UIViewController {
let data = GameDataManager()
override func viewDidLoad() {
super.viewDidLoad()
// when you push to another view controller, point it to the data manager
let someVC = SomeOtherViewController()
someVC.data = data
}
}
class SomeOtherViewController: UIViewController {
var data: GameDataManager?
override func viewDidLoad() {
super.viewDidLoad()
if let word = data?.word {
print(word)
}
}
}
class AnyViewController: UIViewController {
var data: GameDataManager?
}

Update Tab Item Badge after getting data (Swift)

I'd like to update a Badge on a Custom Tab Bar Item when I receive some data. I am able to update the badge on the initial viewDidLoad() but then when I try to call viewDidLoad again later with the data, my tab items are nil. Here is how I have set it up...
class CustomTabBar: UITabBarController {
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
print("this prints correctly every time I call reload with the updated count: " + count)
if let tabItems = self.tabBar.items {
let tabItem = tabItems[0]
tabItem.badgeValue = String(count)
}else{
print("tab items nil")
}
}
func reload(count: Int){
self.count = count
viewDidLoad()
}
}
I'm calling reload() from another view controller after I get the data I need.
func updateBadge(){
let tabBar = CustomTabBar()
let username = UserUtil.username
let db = Firestore.firestore()
let upcomingContestsRef = db
.collection("NBAContests")
.whereField("EnteredUsers", arrayContains: username)
.whereField("Stage", isEqualTo: 2)
upcomingContestsRef.getDocuments()
{
(querySnapshot, err) in
if let err = err
{
print("Error getting documents: \(err)");
}
else
{
print("count is " + String(querySnapshot!.count))
tabBar.reload(count: querySnapshot!.count)
}
}
}
I've check that viewDidLoad is getting called each time in custom tab bar controller, but after the initial load I don't have access to change the tab items anymore.
Does anyone know whats going on?
I've check out these similar questions
Reload / Refresh tab bar items in a ViewController ?
Setting badge value in UITabBarItem in UIViewController
This creates
let tabBar = CustomTabBar()
a new instance instead you need
guard let tabBar = self.tabBarController as? CustomTabBar else { print("returned") ; return }

After my completion handler completes still unable to re-load tableView data swift 4

In my model i've got a function to read in data from firebase.
I call completionHandler(true) when that's done.
This is my viewDidLoad function in my controller that extends UITableViewController.
override func viewDidLoad()
{
super.viewDidLoad()
let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
tableView.backgroundView = activityIndicatorView
self.activityIndicatorView = activityIndicatorView
activityIndicatorView.startAnimating()
model.readInFirebaseData { (success) in
print("data read in")
activityIndicatorView.stopAnimating()
dataArray = model.firebaseDataArray
self.tableView.reloadData()
}
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
}
But the table remains empty and for whatever reason self.tableView.reloadData() isn't populating the table as i'd like but if I segue from the TableViewController and come back the list is populated.
I can't see exactly where i'm going wrong.
Thanks.
Update:
I still couldn't get it working so instead of a completionHandler i used a delegate. What i did was:
Singleton:
protocol Refresh{
func refreshData()
}
var delegate:Refresh?
func readInFirebaseData()
{
self.ref.child("users").observe(DataEventType.childAdded, with: { (snapshot) in
user.name = value?["name"] as? String ?? ""
self.dict.updateValue(user, forKey: uid)
DispatchQueue.main.async {
self.delegate?.refreshData()
print("main thread dispatch")
}
})
}
The TableViewController:
class ListController: TableViewController, Refresh{
viewDidLoad()
{
model.delegate = self
}
func refreshData() {
print("called")
array = model.array
self.tableView.reloadData()
}
That all works. The only issue really that I don't know the answer to is the DispatchQueue.main.async is getting called everytime firebase reads in a "user". But I put it at the end of the readInFirebase function and nothing was populated on the list. But in any case it works at the moment.
Probably readInFirebaseData method is asynchronous and the callback runs on a thread different from the main one.
Remember that all the UIKit related calls must be run on the main thread.
Try with:
model.readInFirebaseData { (success) in
print("data read in")
dataArray = model.firebaseDataArray
DispatchQueue.main.async {
activityIndicatorView.stopAnimating()
self.tableView.reloadData()
}
}
Be sure to set delegate and dataSource:
tableView.delegate = self
tableView.dataSource = self
And make sure you call reloadData() from Main Thread:
DispatchQueue.main.async {
self.tableView.reloadData()
}
Calling it from background threads would typically not lead to your table reloaded, since this operation is UI-related. Any UI related operations should be performed from main thread.

Update tableview instead of entire reload when navigating back to tableview View Controller

I have a home UIViewController that contains a UITableView. On this view controller I display all games for the current user by reading the data from firebase in ViewWillAppear. From this view controller a user can press a button to start a new game and this button takes them to the next view controller to select settings, this then updates the data in firebase and adds a new child. Once they navigate back to the home view controller is there anyway to just update the data with the new child added instead of loading all the games for the table view again as I am currently doing?
This is my current code:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let currentUserID = Auth.auth().currentUser?.uid {
let gamesRef = Database.database().reference().child("games").child(currentUserID)
self.games = []
gamesRef.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let game = child as! DataSnapshot
self.games.append(game)
self.tableView.reloadData()
}
})
}
}
I think you can use observeSingleEvent and .childAdded
You can do the loading of all the data in viewDidLoad and of single child in viewWillAppear since viewDidLoad will be called once initially
Since both methods will be called initially, so we can have a bool flag so we can control which code runs initially and which does not , since viewWillAppear is called after viewDidLoad so we change the value of this flag in viewWillAppear method and then control the execution of code inside viewWillAppear using this flag
class SomeVC: UIViewController {
var flag = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if flag {
//do your work here
}else {
flag = true
}
}
}
Edited:
Another solution can be that you dont do anything in viewDidLoad and do the work only in viewWillAppear since in this particular scenario data in both calls are related (fetching the data from Firebase)
class SomeVC: UIViewController {
var flag = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if flag {
//fetch only one child
}else {
//fetch all the data initially
flag = true
}
}
}

How to detect if a pushed viewcontroller appears again?

assuming I have a viewcontroller (vcA) that pushes QRCodeScannerViewcontroller (vcB). When (vcB) scanned something, It will push ResultviewController (vcC).
-Those 3 views is connected to a UInavigation controller
-the user clicks on the back button on (vcC)
my question is:
1)how can I know if (vcB) is visible without changing code on (vcB)? (vcB) is a pod
2)where will I put this code? I can only access (vcA)
i tried adding this code on (vcA) but nothing happened
override func viewDidDisappear(_ animated: Bool) {
if (vcB.isViewLoaded && (vcB.view.window != nil)){
print("vcb did appear!")
}
}
To know if an instance of cvB's class exists in the navigation stack, you could use this piece of code:
let result = self.navigationController?.viewControllers.filter({
if let vcB = $0 as? UIViewController { // Replace UIViewController with your class, for example ViewControllerB
return true
}
return false
})
if result.isEmpty {
print("An instance of vcB's class hasn't been pused before")
} else {
print("An instance of vcB's class has been pused before")
}