定期后台同步 [英] Periodic background synchronization

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

问题描述

我对iOS编程很陌生,现在想要实现定期后台同步,以便将我的服务器数据与客户端数据同步。我想要达到的目标与Androids SyncAdapter 相当,您可以在其中定义时间间隔(例如每30分钟),系统将在后台自动触发定义的任务。

Im quite new to iOS programming and now want to implement a periodic background synchronization to synchronize my server data with client data. What I want to achieve is comparable with Androids SyncAdapter where you can define a time interval (for example each 30 minutes) and the system will trigger the defined task automatically in the background.

到目前为止,我找不到Swift 3.0的这种机制,所以我需要问一下somone是否有经验或一些提示让我如何实现这一目标。

Until now I could not find such mechanism for Swift 3.0 so I need to ask if somone has experience or some hints for me how I can achieve this.

我想做的事听起来非常简单:

What I want to do sounds quite simple:

当应用程序首次启动时,应用程序应该设置同步管理器每30分钟自动触发一次后台任务。后台任务负责同步服务器和客户端数据(使用Alamofire)。

When the app starts for the first time the app should setup a sync manager which automatically triggers a background task every 30 minutes. The background task is responsible to synchronize server and client data (using Alamofire).

我该怎么做?

推荐答案

有一个名为 BackgroundFetch 的iOS功能,您可以将其设置为

There is an iOS feature called BackgroundFetch, which you can set up to


定期从网络下载和处理少量内容

regularly downloads and processes small amounts of content from the network

您可以设置 minimumBackgroundFetchInterval

与上述Android功能相比,此时间间隔无法保证。

In contrast to the mentioned Android feature, this interval is not guaranteed though.

操作系统在黑盒中做了一些启发式操作。它奖励您使用合理的(对于OS)CPU时间/功耗以及用户经常使用。另一方面,你会因为耗尽电池而受到惩罚,如果你的应用程序从未使用过,那就更糟了。

The OS does some heuristic in a blackbox. It rewards you for using a "reasonable" (to the OS) CPU time/ power consumption and also for being used often by the user. On the other hand you get punished for draining the battery, even worse if your app is never been used.

请参阅: Apple文档

或者,根据您的需要,您可以发布<每当用户数据在服务器端发生变化时,code>静默(推送)通知。静音推送会在不通知用户的情况下唤醒您的应用,因此您可以获取数据并通过安排本地通知来通知用户。

Alternatively, depending on your needs, you can post a Silent (push) Notification whenever the users data changes on server side. A silent push wakes up your app without notifying the user, so you can fetch data and maybe inform the user by scheduling a local notification.

请参阅: Apple文档

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

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