Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get value from inside closure to set to a local variable

I have been trying fetch data from firebase and storing it in a dataModel using completion handler like this


static var dataModels = [DataModel]()

static func fetchData(completion: @escaping () -> Void) {
    let ref = Database.database().reference().child("data")
    ref.observe(
        .value,
        with: { snapshot in
            guard let value = snapshot.value as? [[String: Any]] else {
                return
            }

            for data in value {
                guard let contentArray = data["content"] as? [[String: String]] else {
                    return
                }

                var content = [Content]()

                for item in contentArray {
                    guard let imageURL = item["imageURL"],
                        let title = item["title"],
                        let description = item["description"]
                    else {
                        continue
                    }

                    content.append(
                        Content(imageURL: imageURL, title: title, description: description))
                }

                guard let videoURL = data["videoURL"] as? String,
                    let videoTitle = data["videoTitle"] as? String,
                    let videoDescription = data["videoDescription"] as? String
                else {
                    continue
                }

                let dataModel = DataModel(
                    videoURL: videoURL, videoTitle: videoTitle, videoDescription: videoDescription,
                    content: content)

                DataFetch.dataModels.append(dataModel)
            }

            // Reload your table view after the data has been fetched and appended

            completion()
        })
}

and accessing it like

DataFetch.fetchData {
    self.title.text = DataFetch.dataModels[0].videoTitle
}

but i want to set the number of rows for

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 0

}

I tried by using

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var numberOfRows: Int?
    DataFetch.fetchData {
        number = DataFetch.dataModels[0].content[0].count
    }
    return numberOfRows!
    // Return Found Nil error
}

When I tried to debug it’s coming inside of fetchData but it’s showing nil in return.

Any Solution for this

>Solution :

Refactor your code to

var numberOfRows: Int? 
override func viewDidLoad() {  
   super.viewDidLoad()
   DataFetch.fetchData {
       numberOfRows = DataFetch.dataModels[0].content[0].count
       tableView.reloadData() 
   }
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return numberOfRows ?? 0
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading