我正在创建一个每10秒获取一次新数据并将其插入tableView的应用程序。
func setupTimer(){
var timerInterval : TimeInterval = 10.0
fetchWorldMessagesTimer = Timer.scheduledTimer(timeInterval: timerInterval, target: self, selector: #selector(fetchNewData), userInfo: nil, repeats: true)
}
func invalidateTimer(){
fetchWorldMessagesTimer?.invalidate()
fetchWorldMessagesTimer = nil
fetchWorldMessagesTimer = Timer()
}
每隔10秒调用fetchNewData
。
当用户登录时,我设置了计时器。
工作正常。
现在,如果用户退出,它会使其无效,对.. 但是在再次登录之后,之前失效的计时器再次点火,同时新的计时器也会点火..比如:
19:00:00 Function called
19:00:10 Function called
Logged out
Logged in
19:00:23 Function called // Problem
19:00:23 Function called
19:00:33 Function called
19:00:43 Function called
我该如何解决这个问题?它让我的应用程序感到困惑并使其崩溃..
实际代码:
// added new values to dataArray
// tableViewInserts = new rows that should be added to the tableView
tableView.beginUpdates()
tableView.insertRows(at: tableViewInserts, with: .fade)
tableView.endUpdates()
首次登录时实际输出:
Inserting, time: 1526836900.29165
Before inserting into the tableView...
Number of rows in tableView: 0
Number of rows in dataArray: 7
After inserting...
Number of rows in tableView: 7
Number of rows in dataArray: 7
Inserting, time: 1526836910.08356
Before inserting into the tableView...
Number of rows in tableView: 7
Number of rows in dataArray: 7
After inserting...
Number of rows in tableView: 7
Number of rows in dataArray: 7
第二次登录后的实际输出:
Inserting, time: 1526836998.48148
Before inserting into the tableView...
Number of rows in tableView: 0
Number of rows in dataArray: 7
After inserting...
Number of rows in tableView: 7
Number of rows in dataArray: 7
Inserting, time: 1526836998.48839
Before inserting into the tableView...
Number of rows in tableView: 0
Number of rows in dataArray: 14
2018-05-20 19:23:18.488901+0200 PipeTest[4249:1705076]
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Invalid update: invalid number of rows in section 0.
The number of rows contained in an existing section after the update (14)
must be equal to the number of rows contained in that section before the update (0),
plus or minus the number of rows inserted or deleted from that section (7 inserted, 0 deleted)
and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
答案 0 :(得分:0)
usize