0

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.

1 Answer 1

1

Why don't you create that DataController in callback?

class ViewController: UIViewController {

    var contactData: ContactData!
    var dataController : DataController?
    // this is where am setting the DataController init data currently, but I have to let firebase run first

    override func viewDidLoad() {
            super.viewDidLoad()

            FirebaseAPI.shared.getData(contactID: contact.contactID) { [weak self] result in
                DispatchQueue.main.async {
                    switch result {
                    case .success(let result):
                        dataController = DataController(data: result)
                        self.tableView.dataSource = dataController
                        self.tableView.delegate = dataController
                        self.tableView.reloadData();
                    case .failure(let error):
                        print(error)
                    }

                }
            }
         //.....
    }
}
2
  • Sorry, so I guess the part I left out is there are four dataControllers, and depending on the segmented control, that's how the dataController is chosen, I'll update the code to reflect this. Commented Mar 2, 2018 at 6:46
  • @user3628240 It seems all of the DataControllers are using the same result data, right? So this answer would be a good option as any of the DataControllers could be instantiated within the switch statement. i.e. just put the decision making process about which controller to use within the closure.
    – Jay
    Commented Mar 2, 2018 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.