Application doesn't run in background, but can I still do some task before my application gets terminated?

柔情痞子 提交于 2019-12-08 06:07:29

问题


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


回答1:


My advice would be to try something like this:

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

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];

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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/9221879/application-doesnt-run-in-background-but-can-i-still-do-some-task-before-my-ap

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