Perform background tasks when the application terminates

I am creating an application for my school that should check every n minutes if there is a new tag on the website. To do this the first time a user logs in, the actual label number is stored in "UserDefaults". When the application is completed, after n minutes the number of characters is recounted and compared with the previous one, and sends a notification in case of a change in the number.

What I would like to know is there a way to accomplish this task. I tried to create a timer in -applicationWillTerminate, but it only started once. This is what I tried:

func applicationWillTerminate(_ application: UIApplication) { DispatchQueue.main.async { self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(AppDelegate.findMark), userInfo: nil, repeats: true) self.timer.fire() } } 

The task of SelectMarkMark is the task.

Thanks in advance!

+6
source share
2 answers

You have two options.

  • Background app update
  • Silent push notifications

The easiest option is to update the background application. Because later, a server is required to send a notification. You can check the API to use. Basically, you set the Fetch Background Selection to the Features / Background Modes of your application. Then, from time to time, iOS wakes up your application and calls the application(_:performFetchWithCompletionHandler:) delegate application(_:performFetchWithCompletionHandler:) . You will have about 30-45 seconds to call the function and call completion handler. If you donโ€™t finish it on time, iOS will kill your application. If you do not obey the rules, iOS will give you less chance to wake up. For more detailed use of background modes you can check the following tutorial

+10
source

It is not possible to complete the tasks described in your question after the application terminates. As described in the documentation:

Application close

Applications should be ready for completion at any time and should not wait to save user data or perform other important tasks. System-initiated startups are a normal part of the application life cycle. The system usually shuts down applications so that it can recover memory and make room for other users to start other applications, but the system can also stop applications that incorrectly violate or do not respond to events in a timely manner.

Suspended applications do not receive notifications when they are terminated; the system kills the process and restores the corresponding memory. If the application is currently running in the background and not paused, the system calls applicationWillTerminate: its application delegate until completion. This system is not called when the device is rebooted.

In addition to the system ending your application, the user can explicitly close the application using the multitasking interface. A user-initiated term has the same effect as the termination of a paused application. The application process has been killed and no notification has been sent to the application.

Link: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

Edit:

You cannot complete any task after the application is completed. What you can do is get this calculation from the server and send Push Notification to the device.

+3
source

Source: https://habr.com/ru/post/1012076/


All Articles