I am facing a really mind-breaking problem..
So my application was working just fine, until now..
The app has a TabController: with 5 viewControllers
On this TabController I have a timer that fetches data every 10 seconds
This data is being sent with notifications to one of its child VCs called "WorldMessages"
, which then updates it's tableView
I don't know how or why, but whenever I log out / log in in my application, the WorldMessages ViewController instance get stuck..
So for example after 5 relogin, I have 5 WorldMessages VC in the memory..
// Relogin means that the TabController and its childViews are getting destroyed, and then logging in back creates a new instance of TabController //
I have a clue that it is because of the threading, but I am not sure. When I delete a few lines, it is working okay. Can anyone help me out?
( If i delete those lines in the WorldMessages viewController:
tableView.beginUpdates()
tableView.deleteRows(at: tableViewDeletes, with: .fade)
tableView.insertRows(at: tableViewInserts, with: .fade)
tableView.endUpdates()
Then the app is working fine.. )
TabController, the function that gets called every 10 seconds:
@objc func fetchWorldMessages(scrollToTop: Bool){
worldMessagesFetch.fetchWorldMessages() { response, worldMessageData in
if let response = response {
if response.type == 1 {
// Fetched data
if let worldMessageData = worldMessageData {
DispatchQueue.main.async {
NotificationCenter.default.post(name: .updateWorldMessages, object: nil, userInfo: worldMessageData)
}
}
} else {
// Can not fetch data
self.handleResponses.displayError(title: response.title, message: response.message)
WorldMessagesStore.shared.clear()
NotificationCenter.default.post(name: .reloadWorldMessagesTableView, object: nil)
}
}
}
}
The WorldMessages viewController:
@objc func notification_updateWorldMessages(notification: NSNotification){
self.refreshControl.endRefreshing()
if let newWorldMessages = notification.userInfo?["newWorldMessages"] as? [WorldMessage], let newDeleteArray = notification.userInfo?["newDeleteArray"] as? [Int], let newAppendArray = notification.userInfo?["newAppendArray"] as? [Int], let newWorldMessagesCount = notification.userInfo? ["newWorldMessagesCount"] as? Int, let worldMessagesCount = notification.userInfo? ["worldMessagesCount"] as? Int {
if (newWorldMessagesCount == 0 && noWorldMessages.count == 0){
noWorldMessages = [1]
tableView.reloadSections(IndexSet(integersIn: 1...1), with: .automatic)
} else if (newWorldMessagesCount != 0 && noWorldMessages.count != 0) {
noWorldMessages = []
tableView.reloadSections(IndexSet(integersIn: 1...1), with: .automatic)
}
var count = 0
count = newWorldMessagesCount
if count != 0 {
var tableViewDeletes : [IndexPath] = []
for i in stride(from: count - 1, to: -1, by: -1) {
// WHAT SHOULD BE DELETED
WorldMessagesStore.shared.worldMessages.remove(at: i)
}
var tableViewInserts : [IndexPath] = []
for i in stride(from: count - 1, to: -1, by: -1) {
// WHAT SHOULD BE ADDED
WorldMessagesStore.shared.worldMessages.insert(newWorldMessages[i], at: 0)
}
tableView.beginUpdates()
tableView.deleteRows(at: tableViewDeletes, with: .fade)
tableView.insertRows(at: tableViewInserts, with: .fade)
tableView.endUpdates()
}
}
}
Image:
(In my app "WorldMessages VC" is called Main VC)
So after 2 relogin It get stuck 2 times: (You see 2 instances)
VIDEO of the bug: (demonstrating that if I delete those 4 lines everything works fine)