I am new to Swift, I'm trying to reload my table view after updating data in Swift but it doesn't seems to work. When I change tab and go back the tableView
is reloaded but not automatically. Here is my code:
My TableViewController :
class AllChannelTableViewController: UITableViewController {
var modelChannel = [AllCnannelModel]()
let realm = try! Realm()
lazy var channels: Results<ChannelData> = { self.realm.objects(ChannelData.self) }()
override func viewDidLoad() {
super.viewDidLoad()
defaultChannel()
}
func defaultChannel() {
if channels.count == 0 {
SVProgressHUD.show(withStatus: "dowload")
let URL = "http://52.50.138.211:8080/ChanelAPI/chanels"
Alamofire.request(URL).responseArray { (response: DataResponse<[AllCnannelModel]>) in
let channelArray = response.result.value
if let channelArray = channelArray {
for channel in channelArray {
try! self.realm.write() {
let newChannel = ChannelData()
newChannel.category_id = channel.category_id!
newChannel.idChannel = channel.id!
print(newChannel.idChannel)
newChannel.name = channel.name!
print(newChannel.name)
newChannel.picture = channel.picture
newChannel.url = channel.url!
print(newChannel.url)
self.realm.add(newChannel, update: true)
}
}
}
}
DispatchQueue.main.async{
self.tableView.reloadData()
self.channels = self.realm.objects(ChannelData.self)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// self.tableView.reloadData()
return self.channels.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AllChannelTableViewCell
let strUrl = channels[indexPath.row].picture
cell.nameChannel.text = self.channels[indexPath.row].name
cell.urlChannel.text = self.channels[indexPath.row].url
cell.imageChannel.downloadFrom(url: URL(string: strUrl)!)
SVProgressHUD.dismiss()
return cell
}
}
You need to reload the tableView
after setting the your datasource
array. Also you are currently reloading the tableView
outside the Alamofire
block but it should be inside the Alamofire
request block. So change your reload code like this way.
func defaultChannel() {
if channels.count == 0 {
SVProgressHUD.show(withStatus: "dowload")
let URL = "http://52.50.138.211:8080/ChanelAPI/chanels"
Alamofire.request(URL).responseArray { (response: DataResponse<[AllCnannelModel]>) in
let channelArray = response.result.value
if let channelArray = channelArray {
for channel in channelArray {
try! self.realm.write() {
let newChannel = ChannelData()
newChannel.category_id = channel.category_id!
newChannel.idChannel = channel.id!
print(newChannel.idChannel)
newChannel.name = channel.name!
print(newChannel.name)
newChannel.picture = channel.picture
newChannel.url = channel.url!
print(newChannel.url)
self.realm.add(newChannel, update: true)
}
}
}
DispatchQueue.main.async{
//First set array
self.channels = self.realm.objects(ChannelData.self)
//Now reload the tableView
self.tableView.reloadData()
}
}
}
}