2017-01-30 5 views
3

私は自分のアプリケーションで実行するタイマーを持っています。ユーザーがビューを離れてビューに戻ると、保存とリロードの機能が使用されます。しかし、私は私のアプリ上の別のタブに行く場合、そのタブに必要なアクションは、タイマーを壊すようだ。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() 
    } 
} 

enter image description here

答えて

0

DateFormatterを使用してNotificationsを使用してコントローラ間のデータの受け渡しをミリ秒単位でタイマーを比較してみます。

この機能は、タブを切り替えるたびに呼び出されます。タイマーを無効にし、ティッキングを停止し、時間をミリ秒単位で保存します。

override func viewWillDisappear(_ animated: Bool) { 

    restTimer.invalidate() 

    let df = DateFormatter() 
    df.dateFormat = "y-MM-dd H:m:ss.SSSS" 

    let disappearingDate = Date() 
    let disappearingDateInMilliseconds = df.string(from: disappearingDate) 

    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kViewDidChangeExampleNotification"), object: disappearingDateInMilliseconds) 
} 



/* add your Notification observers in each view controller */ 
override func viewDidLoad() { 
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.handleTimerCallback(notification:)), name: NSNotification.Name(rawValue: "kViewDidChangeExampleNotification"), object: nil) 
} 

あなたは、各コントローラに通知を処理する関数を定義する必要があります。

func handleTimerCallback(notification: NSNotification) { 
    if let dateString = notification.object as? String { 
     print("Object is an String: \(notification.object)") 

     resumeTimerWithDate(dateString) 
    } else { 
     print("Object is not of type String: \(notification.object)") 
    } 
} 

クイックたとえばミリ秒単位でスウィフトの日付を比較する:考慮すべき

func resumeTimerWithDate(dateString: String) { 
    let df = DateFormatter() 
    df.dateFormat = "y-MM-dd H:m:ss.SSSS" 

    let secondDate = Date() 
    let secondDateToStr = df.string(from: secondDate) 

    if dateString < secondDateToStr { 
     print("RESUMING THE TIMER") 
     let resumeDate = df.date(from: dateString) 
     self.createTimer(resumeDate) 
    } else { 
     print("TIMER HAS FINISHED") 
    } 
} 

他のもの:一部の関数は他の関数の前に実行されている可能性があるため、デバッグ/サニティチェックのためにいくつかのprint文を追加することができます。たとえば、viewWillDisappearに通知を投稿するのではなく、viewDidDisappearに投稿するか、prepare(for segue:)を使用して投稿できます。

+0

私はそれを削除しても、タブを変更するとまだフリーズしていますが、意味があります!それについて考えると、残りのタイマーはここで無効にすることを意図しているので、無効にしたときにキャプチャした時間に基づいてタブを再開すると再構築できます – jwarris91

+0

うーん、わかります。 'prepareForSegue'または' NSNotificationCenter'を使って各コントローラにタイマオブジェクトを渡してみましたか? –

+0

私の投稿の編集#1をチェックしてください。 'NotificationCenter'を使ってコントローラ間でデータを渡す方法の簡単な例を示します。 –

関連する問題