iCloud&核心数据:应用程序在后台可能获得NSPersistentStoreDidImportUbiquitousContentChangesNotification? [英] iCloud & Core Data: Possible to get NSPersistentStoreDidImportUbiquitousContentChangesNotification while app is in background?

查看:233
本文介绍了iCloud&核心数据:应用程序在后台可能获得NSPersistentStoreDidImportUbiquitousContentChangesNotification?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的应用程式会根据核心资料记录的活动时间,使用本机通知。

我调用一个名为 updateLocalNotifications()的函数,清除现有的本地通知,并根据Core Data中的更新数据设置新的通知。



我的 NSPersistentStoreCoordinator 为Core Data设置了 NSPersistentStoreUbiquitousContentNameKey ,所以它自动使用iCloud 。



理想情况下,如果用户在两个或更多设备上运行我的应用程序,我想能够运行 updateLocalNotifications / code>在所有设备上的Core Data on iCloud更改。



我在AppDelegate中有这个简单的代码来监听和响应iCloud上的数据更改:

  func应用程序(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - > Bool {

//收到新的iCloud数据时更新本地通知
notificationCenter.addObserver(self,selector:#selector(updateLocalNotifications),name:NSPersistentStoreDidImportUbiquitousContentChangesNotification,object:nil)

...

return true
}

如果应用程序在两个设备上都处于打开状态,并且在一个设备上记录一个事件,我会在大约20秒内获得另一个设备上的 NSPersistentStoreDidImportUbiquitousContentChangesNotification



问题是,当应用程序在后台运行时,我不会收到 NSPersistentStoreDidImportUbiquitousContentChangesNotification 设备。我做了很多挖掘,但似乎不能找到 iCloud同步是否实际上应该是在后台发生,或者只会发生在我的应用程序在前台。我仍然在测试这个,看看它是否只是一个较长的更新间隔,当应用程序在后台。






strong>可能的解决方案#1 (到目前为止,根据测试似乎不可行,但这是我想做的)



我正在寻找一个解决方案,让第二个设备检查iCloud的更改,而它在后台,因此,当有更改并触发此通知时,触发 NSPersistentStoreDidImportUbiquitousContentChangesNotification 即使它的频率比应用程序在前台时更少。



可能的解决方案#2

这个方法肯定有缺点,如果用户在启动应用程序后手动退出, strong>(似乎可能但很复杂)



我正在考虑的另一个可能的解决方案是设置一个服务器,并使用内容 - 可用。这样,当某人在一台设备上记录一个事件时,我会ping服务器并要求它向用户的其他设备发送内容可用推送,我可以调用 updateLocalNotifications()。这有一些缺点。一个是,如果在设备启动或手动退出后没有启动应用程序(类似于解决方案#1), content-available push将不起作用。第二是,每当设备上的事件改变时,即使该信息已经通过iCloud发送到服务器,它还涉及更多的设置服务器和ping服务器的开销。



我发现了一些其他类似的问题(像这样:当应用程序在后台时,如何处理Core Data iCloud同步通知?),但他们不提出任何可能的解决方案,所以我认为值得发布我的情况和可能的解决方案,如果有其他人试图通过类似的问题。

解决方案

我更喜欢这个问题的UX设计解决方案。



用户希望让所有的提醒她的设备。您应该提供一个设置,用户可以将其关闭,或者打开。向用户表明,提醒将是特定于设备的,并且只有在应用处于活动状态时才会更新,而且会根据其他设备的输入进行更新。



对于大多数用户,这将是预期和可接受的行为。


My app uses local notifications based on the timing of events logged with Core Data.

Any time there is a significant change in my Core Data store on the device, I call a function called updateLocalNotifications() that clears existing local notifications and sets up new ones based on the updated data in Core Data.

My NSPersistentStoreCoordinator for Core Data is set up with NSPersistentStoreUbiquitousContentNameKey, so it syncs across devices automatically using iCloud.

Ideally, if the user is running my app on two or more devices, I'd like to be able to run updateLocalNotifications() on all devices whenever the Core Data on iCloud changes.

I have this simple code in AppDelegate to listen for and respond to changes to data on iCloud:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // update local notifications whenever new iCloud data is received
    notificationCenter.addObserver(self, selector: #selector(updateLocalNotifications), name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: nil)

...

    return true
}

If the app is open and in the foreground on both devices and I log an event on one device, I get the NSPersistentStoreDidImportUbiquitousContentChangesNotification on the other device within about 20 seconds.

The problem is I don't ever seem to get NSPersistentStoreDidImportUbiquitousContentChangesNotification when the app is running in the background on the second device. I've done lots of digging, but can't seem to find whether the iCloud sync is actually supposed to be happening in the background, or will only ever happen when my app is in the foreground. I'm still testing this to see if it's just a longer update interval when the app is in the background.


Possible Solution #1 (doesn't seem feasible based on testing so far, but this is what I want to do)

At this point, I'm looking for a solution to get the second device to check iCloud for changes while it's in the background and so a NSPersistentStoreDidImportUbiquitousContentChangesNotification is triggered when there are changes and firing this notification, even if it's less frequently than when the app is in the foreground. There are definitely drawbacks for this approach, like it won't be called if the person manually quits the app after launching it.

Possible Solution #2 (seems possible but complicated)

Another possible solution I'm considering would be to set up a server and use silent push notifications with content-available. That way, when someone logs an event on one device I would ping the server and ask it to send a content-available push to the user's other devices, and I could call updateLocalNotifications() when responding to that push notification. This has a few drawbacks. One is that the content-available push would not work if the app has not been launched after the device is booted or has been manually quit (similar to Solution #1). A second is that it involves a lot more overhead of setting up a server and pinging a server every time the events on a device change, even though that info is already being sent to a server via iCloud.

I've found a few other similar questions (like this one: How can I act on Core Data iCloud sync notification when the app is in the background?), but they don't put forward any possible solutions, so I thought it was worth posting my situation and possible solutions in case there are other people trying to work through a similar problem.

解决方案

I would prefer a UX design solution for this problem.

It is questionable that the user wants to have your reminders pop up on all her devices. You should provide a setting where the user can switch them off, or rather, on. Make it clear to the user that the reminders will be device specific and that they will only be updated when the app is active, but based on the input from other devices.

For most users this will be expected and acceptable behavior.

这篇关于iCloud&核心数据:应用程序在后台可能获得NSPersistentStoreDidImportUbiquitousContentChangesNotification?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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