应用程序不在后台运行,但在应用程序终止之前我仍然可以执行某些任务吗?
我遇到了一个奇怪的情况,客户端不希望他们的应用程序支持多任务处理,因此我在 info.plist
中创建了一个标志,例如,
Application does not run in background = YES;
但是在应用程序生命周期中有时应用程序必须将一些数据上传到服务器在它终止之前。如果不这样做,那么服务器将具有模糊数据,而我客户的公司(可能还有我......)将注定失败!
现在我的问题是,
- 操作系统终止我的应用程序之前还有多少时间?有时,上传的数据可能很大,如果互联网很慢,那么我可能需要更多时间。
- 我看到这个方法 beginBackgroundTaskWithExpirationHandler 适合我的情况。我怀疑如果我确实支持后台执行,这可能会起作用。我说得对吗?
最后的问题,
- 我还能想到其他选择吗?
- 我应该辞去 iOS 开发人员的职务吗?
谢谢
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!!!
Now my questions are,
- How much time do I have before OS will terminate my app? Sometimes data can be large to upload and if internet is slow then I might need more time.
- I saw this method beginBackgroundTaskWithExpirationHandler will this work in my situation. I doubt as this might work if I do support background execution. am I right?
Final questions,
- Any other options I can think of?
- should I resign as a iOS developer?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的建议是尝试这样的事情:
只需将该代码放入类的
init
方法中即可。请务必将自己作为 viewDidUnload 中的观察者删除,如下所示:在这里您可以保存需要保存的任何内容,但我不确定是否上传数据。最好的选择是将数据保存到此处的文件中,然后下次打开应用程序时将数据从文件异步上传到服务器。您可以尝试使用此通知上传它,我只是不确定 Springboard 是否会等待这种情况发生。
My advice would be to try something like this:
Just put that code in the
init
method of your class. Be sure to remove yourself as an observer in viewDidUnload like this: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.
辞职?可能不会,但是在另一家具有现实期望的公司寻找工作?确实。根据我的经验,坚持荒谬的设计理念并且不听劝告的客户更有可能在一切进展顺利时责怪开发商。
至于终止时尝试上传数据。不要这样做。看门狗计时器无法预测您的应用程序在计时器假设它挂起并杀死它之前将获得多少时间。最近,当我开始处理一个已建立的项目时,我遇到了类似的情况,其中应用程序随机无法启动。问题在于开发人员已将下载代码放入 application:didFinishLaunchingWithOptions: 方法中。由于互联网延迟和服务器延迟问题,该方法花费的时间太长,应该将其代码移至后台线程。
尝试在终止、睡眠等方面执行相同的操作也会遇到相同的问题,并且没有已知的方法可以阻止看门狗计时器杀死您的应用程序。
我的另一个问题是为什么客户抵制多任务处理?他们为什么关心?实际上他们无论如何都无法阻止它,因为它实际上是任何应用程序的一部分。我认为,如果他们不想要多任务处理,他们也不想要任何形式的互联网访问或动画。因为如果没有线程化这些东西,你的 UI 很可能无法使用,从而为你赢得很多 1 星评级和投诉。
Resign? probably not, but look for work in another company with realistic expectations? definitely. It's been my experience that clients who insist on rediculous design ideas and won't listen to reason are more likely to blame the developer when it all goes pear shape.
As for trying to upload data when terminating. Don't do it. The watch dog timer is not predictable in how much time your app will get before the timer assumes it is hung and kills it. I hit a similar situation recently when I started working on a established project where the app was randomly failing to start. The problem was that the developers had put download code into the application:didFinishLaunchingWithOptions: method. Because of internet latency and server latency issues, the method was taking too long and should have had it's code moved to a background thread.
Trying to do the same thing on termination, sleep, etc will have the same problem and there is no known way to stop the watchdog timer from killing your app.
The other question I have is why is the client resistant to multitasking? why do they care? They actually cannot stop it anyway because it's effectively part of any app. I presume that if they don't want multitasking they also don't want any form of internet access or animations. Because without threading those things, your UI is very likely to be unuseable, earning you a lot of 1 star ratings and complaints.
什么时候将数据上传到服务器有关系吗?它必须在应用程序终止之前正确吗?
如果没有,那么我有这个建议:
在应用程序委托方法 applicationWillTerminate 中,将所有数据写入应用程序沙箱文件系统的文本文件,例如 mydata.txt(即在 Library/Cache 目录中。不能使用应用程序生成的文件的文档目录。Apple 现在使用文档目录进行 iCloud 同步,因此将文件放在那里将导致应用程序被拒绝)。还需要将您的文件标记为不同步到 iCloud 属性。
然后,在下次启动应用程序时,您可以检查此“mydata.txt”文件是否存在。如果存在,请从中读取所有数据并上传到您的服务器。然后从 Library/Cache 文件夹中删除该文件,这样下次就不会上传相同的数据。您可以在应用程序委托的 applicationDidFinishLaunching 方法中执行此操作。
Does it matter when you upload the data to the server? Does it have to be right before application terminates?
If not, then I have this suggestion:
In the App Delegate method applicationWillTerminate, write all your data into a text file, something like mydata.txt, of your application sandbox filesystem (namely in the Library/Cache directory. Can't use the Document directory for app generated files. Apple now uses Document directory for iCloud syncing so putting your files there will result in app being rejected). Also need to mark your file with don't sync to iCloud attribute.
Then on the next launch of your application, you can check for the existence of this "mydata.txt" file. If it exists, read all the data from it and upload to your server. Then delete the file from the Library/Cache folder so next time it doesn't upload the same data. You can do this in the App Delegate's applicationDidFinishLaunching method.