Delete data when app is killed in the background.

陌路散爱 提交于 2019-12-02 15:36:00

问题


I have an app where I am saving data but when the app is killed in the background I have to delete all the saved data.

I tried using this method:

- (void)applicationWillTerminate:(UIApplication *)application

But its not working. Can someone please suggest me how to do that?


回答1:


Not sure why you need to wipe the data on terminate specifically, but here is something to consider:

Another way to handle this type of situation when apps write data that may be incomplete when they are forced to quit, is that they write out a flag when the data is known to be good.

That way, if the app exits normally, the data will be written and the flag will be written.

If the app is forced to quit, the flag will not get written by nature of the forced quit.

THEN, when the app starts it can look for the flag. If the flag isn't there, the app knows that any data is incomplete and can discard it (delete it) and start over.

Hope this helps.




回答2:


applicationWillTerminate: is basically never called and you should no longer rely on it.

The app is not given any callback when it is killed so you can't do anything. You need to decide on a different approach based on the actual user requirement. This may involve encryption / not holding certain data on disk (only ever in memory) / etc...




回答3:


I know, I'm super late to the party but, if you can live with data "sticking around" until the next app launch you can delete/clear data in application(_:didFinishLaunchingWithOptions).



来源:https://stackoverflow.com/questions/22477220/delete-data-when-app-is-killed-in-the-background

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