iOS 定时后台处理 [英] iOS timed background processing

查看:25
本文介绍了iOS 定时后台处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用能够 - 例如,每 12 小时 - 即使应用当前没有运行或在后台,向服务器发送 HTTP 请求,获取带有版本的小文件编号,如果服务器上的版本高于客户端上的版本,则将更多文件下载到磁盘,以便下次启动应用程序时它会在磁盘上找到新内容.

I'd like my app to be able to - say, every 12 hours - even if the app isn't curently running or is in the background, send an HTTP request to a server, get a small file with a version number and if the version on the server is higher than the version on the client, then download some more files to disk so that next time the app starts it will find new content on disk.

什么设计模式最适合 ios 中的此类任务?

What design patterns are best suited for such task in ios?

我想到了一些,但我没有那么有经验.

A few come to my mind but I'm not as experienced.

  1. 也许是推送通知,当有新版本可用时,服务器需要向所有客户端推送消息.
  2. 或者,有没有类似于 Android 的 Service 的东西可以提供帮助?
  3. 或者,也许每次应用启动(或进入前台)时,只需 ping 服务器并查看是否有任何新内容.
  4. 或者,每次应用开始对服务器执行 ping 操作,并在接下来的 12 小时内添加一个计时器,以防此应用仍处于前台.
  5. 或者,每次应用启动时,检查一个首选项值,如果上次 ping 服务器是 12 小时前,那么现在就 ping 它.然后保存这个ping时间.
  1. Perhaps push notifications, the server needs to push a message to all clients when there's a new version available.
  2. Or, Is there something similar to Android's Service that can help?
  3. Or, maybe every time the app starts (or goes to the foreground) just ping the server and see if there's anything new.
  4. Or, every time the app starts ping the server plus add a timer for the next 12h in case this app will still be in the foreground.
  5. Or, every time the app starts, check a preference value and if the last time the server was pinged was more than 12h ago, then ping it now. And then save this ping time.

选项 1 在服务器上可能更重,实施起来可能更复杂(考虑 ios newbe),但可能是真正后台更新的唯一选项.但即便如此,我仍然不希望用户必须对一些低级数据更新 b/wa 客户端和服务器(就是这样)做出反应,所以除非推送通知可以直接转到应用程序并执行没有用户干预的东西,那么这个选项就不会生效.
选项 3-5 都是可能的,听起来不太难,但它们仅在应用程序处于前台时才有效.

Option 1 may be more heavy on the server and could be more complicated to implement (consider an ios newbe) but may be the only option for real background update. But even so, I still don't want the user to have to react to some low-level data update b/w a client and a server (and that's what it is), so unless push notifications can go directly to the app and execute something without the user's intervention then this option doesn't fly.
Options 3-5 are all possible and don't sound too hard but they would only work while the app is in the foreground.

据我所知,后台应用程序只能播放音乐、获取位置更新或 voip 更新.甚至还有 这个 hack 试图摆脱这个限制的无声声音.(并且未获准入店).

From what I know, background apps can only either play music, get location updates or voip updates. There's even this hack with the silent sound that was trying to escape this limitation. (and was not approved to the store).

也许现有的限制是有充分理由的,那么我该如何遵守规则并能够实现周期性的服务器 ping(或更一般地说,即使应用程序也能解决客户端和服务器的周期性同步黑白问题)在bg)?

Perhaps the limitations in place are of good cause, so how do I play by the rules and be able to achieve a periodic server ping (or more generally, solve the problem of periodic sync b/w clients and servers even when apps are in bg)?

谢谢

推荐答案

我对你的问题没有明确的答案,只是一些漫不经心的评论,可以帮助你决定什么最适合你的情况.抱歉,这是我能提供的最好的了.

I don't have a definite answer for your question, just a rambling set of comments that may help you decide what will work best for your situation. Sorry, this is the best I can offer.

要记住的一点是,应用不应该在不让用户知道它正在下载某些东西的情况下使用手机的任何数据计划配额.有些应用程序都是关于下载诸如 Twitter 客户端之类的东西,因此应用程序的性质告诉用户该应用程序正在使用数据计划.其他应用程序,如绘图程序,几乎没有明确的下载需求,因此应通知用户需要下载.

One thing to keep in mind is that an app shouldn't use any of the phone's data-plan quota without letting the user know it's downloading something. Some apps are all about downloading stuff such as Twitter clients, so the nature of the app tells the user the app is using the data plan. Other apps, like a drawing program, have little explicit need for downloading, so should notify the user of a need for a download.

由于 Apple 不允许开发人员选择在后台下载,因此使用 iOS 的用户被训练等待他们的应用下载更新的数据.在等待下载时改善用户体验的典型方法是至少显示一个微调器,让用户知道应用程序正在运行.要进一步改进界面,请将下载发送到另一个线程,并允许人们继续使用应用程序的其余部分.他们可以与旧数据交互,或使用应用程序中不需要更新的部分.

Because Apple doesn't allow developers the option of downloading in the background, people using iOS are trained to wait for their apps to download updated data. The typical way to improve the user experience while waiting for a download is to at a minimum show a spinner, letting the user know the app is working. To improve the interface even more, send the download to another thread, and allow the people to continue to use the rest of the app. They can interact with the old data, or use the parts of the app that are not in need of an update.

Apple 没有为程序员提供在后台为大多数应用类型下载新内容的机制.根据苹果的公告,iOS 5 的报亭功能将允许在后台更新订阅.或许未来我们开发者会有更多的后台下载选择.

Apple doesn't give programmers a mechanism to download new content in the background for most app types. According to Apple's announcements, iOS 5's Newsstand feature will allow subscriptions to be updated in the background. Maybe in the future we developers will have more options for background downloading.

我在应用商店中有一个使用方法 5 的应用,而另一个正在使用方法 3 的应用.

I have one app on the app store that uses method 5, and another in the works that uses method 3.

如果人们想尽快知道有新数据可用,我会使用推送通知(方法 1).这将取决于主题.

I'd use push notifications (method 1) if the people would want to know as soon as possible that new data is available. It would depend upon the topic.

iOS 没有类似 Android 的服务(方法 2)

iOS doesn't have anything like Android's service (method 2)

我有一个应用程序,每次启动应用程序时都会检查 RSS 提要中的新闻(方法 3).这个应用程序主要做其他事情,但在起始视图上显示提要.由于该应用程序是一个可以帮助人们找到特定解决方案的简单实用程序,因此 RSS 提要是辅助性的.

I have an app that checks an RSS feed for news each time the app is launched (method 3). This app mostly does other things, but shows the feed on the starting view. Since the app is a simple utility that helps people find a specific solution, the RSS feed is ancillary.

我喜欢方法 4 中的计时器想法.如果您想让该人有机会批准下载,计时器可以弹出警报视图,然后等待.这样,如果设备只是将您的应用放在前台,则该应用实际上不会下载任何内容.

I like the timer idea in method 4. If you want to give the person a chance to approve the download, the timer could pop up an alert view, and then wait. That way the app doesn't actually download something if the device was just left sitting with your app in the foreground.

我在当前可用的应用程序中实现的方法 5 有一些变化.它仅下载众多视图之一的数据.每次访问此视图时,它都会检查存储的时间以查看是否应该下载新数据.然后它请求许可.

My implementation of method 5 in my currently available app has a little variation. It downloads the data for just one of many views. Each time this view is visited it checks against the stored time to see if it should download fresh data. Then it asks for permission.

这篇关于iOS 定时后台处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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