私は自分のアプリケーションで実行するタイマーを持っています。ユーザーがビューを離れてビューに戻ると、保存とリロードの機能が使用されます。しかし、私は私のアプリ上の別のタブに行く場合、そのタブに必要なアクションは、タイマーを壊すようだ。TImerを破るいくつかのアプリケーションの動作?
これは、アプリケーションでのスレッドの使用とタイマーカウントを壊す他のタブのコアデータによるものでしょうか?ここに私のタイマーコードがアップデート2
次のとおりです。ここに私の改定タイマーコードはコードが破損の原因となっている間に別のVC IDスイッチで
// MARK: - SETTING UP SETS & TIMERS
func createStopTimeForRestFromUserTime(userTime: Int) -> Date {
let calendar = Calendar.current
let stopDate = calendar.date(byAdding: .second, value: userTime, to: Date())
return stopDate!
}
func createTimer(stopDate: Date) {
print("CONSTRUCTING A TIMER")
userDefaults.set(stopDate, forKey: "setStopDate")
restTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(RestController.updateRestTimer), userInfo: nil, repeats: true)
}
func updateRestTimer() {
let presentTime = Date()
let stoppedTime = UserDefaults.standard.object(forKey: "setStopDate") as? Date
if stoppedTime?.compare(presentTime) == ComparisonResult.orderedDescending {
restRemainingCountdownLabel.text = dateComponentsFormatter.string(from: presentTime, to: stoppedTime!)
} else {
self.stopTimer()
print("THE TIMER IS NOW FINISHED")
}
}
func stopTimer() {
self.restTimer.invalidate()
}
// MARK: - CONFIGURE TIMER ON OPEN/CLOSE
override func viewWillDisappear(_ animated: Bool) {
print("VIEWWILLDISAPPEAR WAS CALLED")
stopTimer()
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ViewDidChangeNotification"), object: NSDate())
}
func handleResumedTime(disappTime: NSDate) {
let disappearedTime = disappTime
let resumeTime = NSDate()
if disappearedTime.compare(resumeTime as Date) == ComparisonResult.orderedDescending {
print("RESUMING THE TIMER")
self.createTimer(stopDate: disappearedTime as Date)
} else {
print("TIMER HAS FINISHED")
stopTimer()
}
}
func handleTimerCallback(notification: NSNotification) {
if let date = notification.object as? NSDate {
self.handleResumedTime(disappTime: date)
}
}
...いっぱいである:
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(RoutineController.handleTimerCallback(notification:)), name: NSNotification.Name(rawValue: "ViewDidChangeNotification"), object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ViewDidChangeNotification"), object: NSDate())
}
func handleTimerCallback(notification: NSNotification) {
let date = notification.object
print("Object is \(date)")
}
編集:タブを変更してもう一度元に戻すときに、更新されたhandleResumed funcとコンソール出力が追加されました...
func handleResumedTime(disappTime: NSDate) {
let disappearedTime = disappTime as Date
let resumeTime = NSDate() as Date
print("ATTEMPTING RESUME")
print(disappearedTime)
print(resumeTime)
if resumeTime.compare(disappearedTime) == ComparisonResult.orderedAscending {
print("RESUMING THE TIMER")
self.createTimer(stopDate: disappearedTime)
} else {
print("TIMER HAS FINISHED")
stopTimer()
}
}
私はそれを削除しても、タブを変更するとまだフリーズしていますが、意味があります!それについて考えると、残りのタイマーはここで無効にすることを意図しているので、無効にしたときにキャプチャした時間に基づいてタブを再開すると再構築できます – jwarris91
うーん、わかります。 'prepareForSegue'または' NSNotificationCenter'を使って各コントローラにタイマオブジェクトを渡してみましたか? –
私の投稿の編集#1をチェックしてください。 'NotificationCenter'を使ってコントローラ間でデータを渡す方法の簡単な例を示します。 –