定时器的功能在失效后被调用

时间:2022-05-20 16:02:38

I am making an application that fetches new data every 10 seconds and inserts it to a tableView.

我正在创建一个每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()
    }

It calls fetchNewData every 10 seconds.

它每10秒调用一次fetchNewData。

When a user logs in, I setup the timer.

当用户登录时,我设置了计时器。

It works fine.

它工作正常。

Now, If the user logs out, it invalidates it, right.. But after logging in again, the previous invalidated timer Fires one more time, and the same time the new timer fires too.. Like:

现在,如果用户注销,它会使其无效,正确..但在再次登录后,先前失效的计时器再次触发,同时新计时器也会触发..同样:

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

How could I fix this? It confuses my app and gets it crashed..

我怎么能解决这个问题?它混淆了我的应用程序并使其崩溃..


Actual code:

// 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()

Actual output first time logging in:

实际输出首次登录:

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

Actual output after second login:

第二次登录后的实际输出:

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).'

1 个解决方案

#1


0  

var timer: Timer?

func startTimer() {
    let timerInterval : TimeInterval = 0.5
    timer = Timer.scheduledTimer(timeInterval: timerInterval, target: self, selector: #selector(fetchNewDate), userInfo: nil, repeats: true)
}

func invalidateTimer() {
    timer?.invalidate()
    timer = nil
}

@objc func fetchNewData() {
    print("New Data")
}

#1


0  

var timer: Timer?

func startTimer() {
    let timerInterval : TimeInterval = 0.5
    timer = Timer.scheduledTimer(timeInterval: timerInterval, target: self, selector: #selector(fetchNewDate), userInfo: nil, repeats: true)
}

func invalidateTimer() {
    timer?.invalidate()
    timer = nil
}

@objc func fetchNewData() {
    print("New Data")
}