应用程序不在后台运行,但是在终止应用程序之前我还能做些任务吗? [英] Application doesn't run in background, but can I still do some task before my application gets terminated?

查看:77
本文介绍了应用程序不在后台运行,但是在终止应用程序之前我还能做些任务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况很奇怪,客户端不希望其应用程序支持多任务处理,因此我在info.plist中创建了一个标记,例如

I have a weird situation, client does't want their application to support multitasking so I created a flag in info.plist like,

Application does not run in background = YES;

但是,在应用程序生命周期中,有时应用程序必须在终止之前将一些数据上传到服务器.如果不这样做,那么服务器将具有模糊数据,那么我的客户的公司(可能还有我的公司)将注定要失败!!!

However there are times during application lifecycle that the application must upload some data to server before it terminates. If it doesn't then server will have fuzzy data and my client's company (and probably me too...) will be doomed!!!

现在我的问题是,

  • 我需要多少时间操作系统才能终止我的应用程序?有时候,上传的数据可能很大,如果互联网速度慢,那么我可能需要更多时间.
  • 我看到此方法beginBackgroundTaskWithExpirationHandler将在我的情况下正常工作.我对此表示怀疑,因为如果我确实支持后台执行,这可能会起作用.我说的对吗?

最后的问题

  • 我能想到的其他选择吗?
  • 我应该辞职为iOS开发人员吗?

谢谢

推荐答案

我的建议是尝试这样的事情:

My advice would be to try something like this:

[[NSNotificationCenter defaultCenter] 
 addObserver:self 
 selector:@selector(saveWhatever) 
 name:UIApplicationWillTerminateNotification
 object:nil];

只需将代码放入类的init方法中即可.确保像这样在viewDidUnload中将自己删除为观察者:

Just put that code in the init method of your class. Be sure to remove yourself as an observer in viewDidUnload like this:

[[NSNotificationCenter defaultCenter] removeObserver:self];

您可以在此处保存任何需要保存的内容,但是我不确定要上传数据.最好的选择是将数据保存到此处,然后在下次打开应用程序时,将数据从文件异步上传到服务器.您可以尝试使用此通知上载它,但我不确定Springboard是否会等待这种情况的发生.

Here you can save whatever you need to save, but I'm not sure about uploading data. Your best bet is to save the data to a file here, and then next time the app is opened upload the data from the file to the server asynchronously. You can try uploading it with this notification, I'm just not sure Springboard is going to wait around for that to happen.

这篇关于应用程序不在后台运行,但是在终止应用程序之前我还能做些任务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆