Reputation: 5167
I have a SectionsData.swift
file where I get some data from a get http request:
import Foundation
import Alamofire
class SectionsData {
func getSectionsFromData() -> [Section] {
var sectionsArray = [Section]()
let animals = Section(title: "Animals", objects: ["Cats", "Dogs", "Lions", "Birds"], place: "Jungle")
let vehicles = Section(title: "Vehicles", objects: ["Cars", "Bicycle"], place: "Road")
let movies = Section(title: "Movies", objects: ["Sound", "Music"], place: "Cinema")
sectionsArray.append(animals)
sectionsArray.append(vehicles)
sectionsArray.append(movies)
Alamofire.request(.GET, "https://example.com")
.responseJSON { response in
if let JSON = response.result.value {
var openEvents = Section(title: "Events", objects: [], place: "Rooms")
for index in 0...9 {
let eventName = JSON["events"]!![index]["name"]! as! String
openEvents.items.append(eventName)
}
sectionsArray.append(openEvents)
}
}
return sectionsArray
}
}
And in the ViewController.swift
file, I'm trying to reload the view after the JSON request is complete:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var sections: [Section] = SectionsData().getSectionsFromData()
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sections.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section].heading
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].items.count
}
...
Currently, I only see the static data with table view sections for "Animals", "Vehicles" and "Movies" and no "Events" section.
I believe I will need to reload the view somewhere. How can I do so?
Upvotes: 1
Views: 1478
Reputation: 2771
First of all, inside your getSectionsFromData()
you're calling an asynchronously call to Alamofire.request()
which will not be executed in due time for the sectionsArray
to have the returned data. You're in fact returning the static array from getSectionsFromData
.
To fix this you could use a callback in the getSectionsFromData()
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var sections: [Section] = []
override func viewDidLoad() {
super.viewDidLoad()
SecionsData().getSectionsFromData({
sections in
self.sections = sections
self.tableView.reloadData()
})
}
}
class SectionsData {
func getSectionsFromData(callback: (sections: [Section]) -> ()) {
var sectionsArray = [Section]()
let animals = Section(title: "Animals", objects: ["Cats", "Dogs", "Lions", "Birds"], place: "Jungle")
let vehicles = Section(title: "Vehicles", objects: ["Cars", "Bicycle"], place: "Road")
let movies = Section(title: "Movies", objects: ["Sound", "Music"], place: "Cinema")
sectionsArray.append(animals)
sectionsArray.append(vehicles)
sectionsArray.append(movies)
Alamofire.request(.Get, "https://example.com")
.responseJSON { response in
if let JSON = response.result.value {
var openEvents = Section(title: "Events", objects: [], place: "Rooms")
for index in 0...9 {
let eventName = JSON["events"]!![index]["name"]! as! String
openEvents.items.append(eventName)
}
sectionsArray.append(openEvents)
}
// This will pass the sections with or without any fetched data into the callback
callback(sections: sectionsArray)
}
}
}
Upvotes: 4