I have a view controller in which I want to make a firebase pull, and then I want to push the data from the firebase pull into a "DataController" that transforms the data so it fits into the UITableViewDelegate. The problem is that there are four DataControllers, and a segmented control selects which of them is being used.
How do I make it so that the firebase function runs first in ViewDidLoad() and I can pass a value into the DataController.
Relevant lines of code are below:
class ViewController: UIViewController {
var contactData: ContactData!
private var dataControllers: [DataController] = [
DataController1(data:contactData),
// this is where am setting the DataController init data currently, but I have to let firebase run first
DataController2(),
DataController3(),
DataController4()
]
private var segment: Segment = .profile {
didSet {
let dataController = self.dataControllers[self.segment.rawValue]
self.tableView.dataSource = dataController
self.tableView.delegate = dataController
self.tableView.reloadData()
}
}
self.tableView.dataSource = dataController
self.tableView.delegate = dataController
override func viewDidLoad() {
super.viewDidLoad()
FirebaseAPI.shared.getData(contactID: contact.contactID) { [weak self] result in
DispatchQueue.main.async {
switch result {
case .success(let result):
self?.contactData = result
case .failure(let error):
print(error)
}
}
}
//.....
}
How would I feed contactData into DataController(data: ContactData)? Right now the firebase code is running last in ViewDidLoad().
Sorry for the basic question, I am still learning Swift and Firebase.