简体   繁体   English

iCloud和核心数据:当app在后台时,可以获得NSPersistentStoreDidImportUbiquitousContentChangesNotification吗?

[英]iCloud & Core Data: Possible to get NSPersistentStoreDidImportUbiquitousContentChangesNotification while app is in background?

My app uses local notifications based on the timing of events logged with Core Data. 我的应用程序根据使用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. 只要设备上的Core Data存储发生重大变化,我就会调用一个名为updateLocalNotifications()的函数来清除现有的本地通知,并根据Core Data中的更新数据设置新的通知。

My NSPersistentStoreCoordinator for Core Data is set up with NSPersistentStoreUbiquitousContentNameKey , so it syncs across devices automatically using iCloud. 我的NSPersistentStoreCoordinator for Core Data使用NSPersistentStoreUbiquitousContentNameKey设置,因此它使用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. 理想情况下,如果用户在两个或更多设备上运行我的应用程序,我希望能够在iCloud上的核心数据发生更改时在所有设备上运行updateLocalNotifications()

I have this simple code in AppDelegate to listen for and respond to changes to data on iCloud: 我在AppDelegate中有这个简单的代码来监听和响应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. 如果应用程序在两台设备上都处于打开状态并处于前台,并且我在一台设备上记录了一个事件,那么我会在大约20秒内在另一台设备上获得NSPersistentStoreDidImportUbiquitousContentChangesNotification

The problem is I don't ever seem to get NSPersistentStoreDidImportUbiquitousContentChangesNotification when the app is running in the background on the second device. 问题是,当应用程序在第二个设备的后台运行时,我似乎永远不会得到NSPersistentStoreDidImportUbiquitousContentChangesNotification 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 . 我已经做了很多挖掘,但似乎无法确定iCloud同步是否实际上应该在后台发生,或者只会在我的应用程序位于前台时发生 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) 可能的解决方案#1 (到目前为止基于测试似乎不可行,但这是我想要做的)

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. 此时,我正在寻找一种解决方案,让第二台设备检查iCloud是否在后台进行更改,因此当有更改并触发此通知时会触发NSPersistentStoreDidImportUbiquitousContentChangesNotification ,即使它比应用程序时更少在前台。 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) 可能的解决方案#2 (似乎可能但很复杂)

Another possible solution I'm considering would be to set up a server and use silent push notifications with content-available . 我正在考虑的另一个可能的解决方案是设置服务器并使用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. 这样,当有人在一台设备上记录事件时,我会ping服务器并要求它向用户的其他设备发送content-available推送,并且我可以在响应该推送通知时调用updateLocalNotifications() 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). 一个是如果在设备启动或手动退出后尚未启动应用程序,则content-available推送将无效(类似于解决方案#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. 第二个问题是,每次设备上的事件发生变化时,设置服务器和ping服务器都会产生更多的开销,即使该信息已经通过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. 我发现了一些其他类似的问题(比如这个问题: 当应用程序在后台时,我如何对Core Data iCloud同步通知采取行动? ),但他们没有提出任何可能的解决方案,所以我认为它是值得发布我的情况和可能的解决方案,以防有其他人试图解决类似的问题。

I would prefer a UX design solution for this problem. 我更喜欢这个问题的UX设计解决方案。

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. 对于大多数用户来说,这是预期和可接受的行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 核心数据+ iCloud Sync NSPersistentStoreDidImportUbiquitousContentChangesNotification - Core data + iCloud Sync NSPersistentStoreDidImportUbiquitousContentChangesNotification 带iCloud的CoreData:在NSPersistentStoreDidImportUbiquitousContentChangesNotification上接收空数据 - CoreData with iCloud: Receiving empty data on NSPersistentStoreDidImportUbiquitousContentChangesNotification 当监听NSPersistentStoreDidImportUbiquitousContentChangesNotification时,iCloud如何将通知发送到我的应用程序 - How iCloud sends a notification to my app when listening NSPersistentStoreDidImportUbiquitousContentChangesNotification 仅在应用启动后立即调用iCloud NSPersistentStoreDidImportUbiquitousContentChangesNotification - iCloud NSPersistentStoreDidImportUbiquitousContentChangesNotification only called right after app launch 在应用程序运行时启用/禁用Core Data的iCloud同步 - Enable / disable iCloud sync of Core Data while app is running 将Core Data App迁移到iCloud - Migrate Core Data App to iCloud 带有核心数据,照片和iCloud的iOS应用 - iOS app with core data, photos & iCloud 核心数据和 iCloud 同步在实时应用程序中不起作用 - Core data and iCloud sync not working in live App “带有 iCloud 的核心数据”与“iCloud 核心数据” - “Core Data with iCloud” vs “iCloud Core Data” 是否可以将iCloud中的Core Data用作服务器,将iPad用作客户端? - Is it possible to use Core Data in iCloud as server, and the iPad's as clients?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM