Home>

I want to keep measuring time like a stopwatch with swift.

However, if you start measuring time on the actual device and switch to the background for a while, you will return to the first screen that opened the app. Why is this?
By the way, I edited only the part of AppDelegate.swift with reference to this site.
https://qiita.com/SatoTakeshiX/items/8e1489560444a63c21e7

Even if I couldn't keep measuring the time, I would like to do something just to return to the state when the app opened before the screen transitions after returning from the background after a certain time. . .

I was wondering if I could do something like the default app stopwatch, but that seems special. Like that default app, I want to keep the elapsed time on the screen even if I go to the background or close it, but it is basically useless to move in the background for a long time So, what about the following flow?

  1. Save time when you start measuring locally
  2. Retrieve the current time for processing when resumed from the background.
  3. The difference between the current time and the start time is calculated and the count start&display again

The flow is as above, but did you decide whether it was in the background or the foreground? I thought it would be possible to implement it in the above flow if that was possible.

  • Answer # 1

    Continuing background processing is not recommended by Apple as Hayato recognizes. . .
    https://qiita.com/teamhimeH/items/51efb4fc68d3f1d2ebb9

    It seems that the background task execution time is up to about 180 seconds. (There are various theories?)

    >>I want to keep the elapsed time on the screen even if I go to the background or close it
    I think this can be achieved with the logic that Hayato thinks.
    If you examine the life cycle of AppDelegate, you will get a lot of information.
    https://qiita.com/mfujiwara/items/cf3d61a96cf4fdfc1162

    After returning from the background of the main theme, the screen will return to the screen when the app was first launched. Is there any app crashing?
    I would like you to debug with the actual machine and check if there is an error.
    Basically, switching the app's foreground or background may cause the screen to return to the screen when the app is first launched.
    Other possibilities are
    -Because the memory of the real machine was imminent, the application task was killed at the OS level. (Maybe less likely)
    -In the process of returning to the application foreground, the process of displaying the screen when the application is first started has been executed.
    Is it? . .