SWIFT: how to run a function even when user kills the app

落爺英雄遲暮 提交于 2019-12-12 19:16:44

问题


I am using swift 2 and Xcode7 for iOS9. I want to know if I can maintain a function (that checks for something to delete) running "forever" even if the user kills the app?

I am deleting contacts from the contact list according to some rules and time. It is running ok, but just with the app opened or in second plan. I want to make this app capable to delete those contacts even when the user kills it.


回答1:


You can use background thread when user opens the app. But if the app will be terminated, there is no option to run functions. Look for the app lifecycle here and redesign your architecture: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html




回答2:


If the user kills the app it is no longer running, therefore your code is no longer running. There is no such state that your code/app can be in where this is possible.

By "kill", I don't mean "background". Backgrounding an app is different. Check Apple's docs on the different app states (see m.albin's answer) as well as various strategies for handling those app states.



来源:https://stackoverflow.com/questions/33900590/swift-how-to-run-a-function-even-when-user-kills-the-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!