CloudKit 仅在模拟器和第一台设备中数据未在第二台设备中更新

CloudKit data not updating in second device only in simulator and first device

我正在努力使来自 CloudKit 的数据在多台设备上保持同步。使用以下代码,我可以成功添加、加载并在 CloudKit 中修改记录时收到通知,我的问题是这仅在我使用模拟器修改数据并使用物理设备查看时有效通知。

下面是我的使用方法。

  1. 我在模拟器和 iPhone 中安装了该应用程序。

  2. 从模拟器中,我去输入一个新的 note/record,然后我在 iPhone 中收到通知,这就是我想要的。我也可以直接从 CloudKit dashboard 输入记录,我也可以在 iPhone.

  3. 中收到通知
  4. 第三步无效的是...我去了我妻子的 iPhone,从 Xcode 安装了应用程序并登录 iTunes & App Store 使用我在模拟器和数据更新的第一个设备中使用的相同帐户,然后我在模拟器和第一个 iPhone 中输入数据,但我没有收到我妻子的任何通知iPhone。我尝试直接在我妻子的 iPhone 中输入数据,它似乎保留了数据,因为我可以退出应用程序并且数据不会丢失。所以,我最终在两个 phone 中得到完全不同的数据,在我妻子的 iPhone 中,我只看到直接输入 phone 和我的第一个 phone 中的内容,我在模拟器或 CloudKit 仪表板中看到了直接输入的内容。

我可能误解了整个生态系统及其运作方式。我所期待的是,每个安装了该应用程序并使用相同 iTunes & App Store 登录凭据登录的 phone 都会在所有设备中看到更新。

我误解了什么?

这是代码:

主要ViewController

添加记录

public func saveNotestoCloud(){
    let privateDatabase = CKContainer.default().database(with: .private)
    if let title =  self.textfield.text, let body = textView.text, let record = self.note?.record{
        record["title"] = title
        record["note"] = body
        self.note?.record = record

        privateDatabase.save(record) { (record,error) in
            if let error = error{
                print("Error  \(error.localizedDescription)")
                return
            }
            print("Successfully saved record \(record?.recordID.recordName ?? "")")
        }
    }
}

应用委托

正在注册通知

let privateDatabase = CKContainer.default().database(with: .private)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    self.createSubscriptions()

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]){ (granter, error) in
        guard error == nil else{
            print(error!)
            return
        }
    }
    application.registerForRemoteNotifications()
    return true
}

private func createSubscriptions(){

    let predicate = NSPredicate(value: true)
    let subscription = CKQuerySubscription(recordType: "Notes",
                                                       predicate: predicate,
                                                       subscriptionID: "SubscriptionID",
                                                       options: [CKQuerySubscription.Options.firesOnRecordCreation,  CKQuerySubscription.Options.firesOnRecordDeletion, CKQuerySubscription.Options.firesOnRecordUpdate])

    let notificationInfo = CKSubscription.NotificationInfo()
    notificationInfo.alertLocalizationKey = "New Note"
    notificationInfo.shouldBadge = true
    subscription.notificationInfo =  notificationInfo
    let privateData = CKContainer.default().database(with: .private)
    privateData.save(subscription){ result, error in
        if let error = error {
            print(error.localizedDescription)
        }
    }
}

据我了解,CloudKit依赖于设备设置中在iCloud下登录的账户,不是 iTunes 和 App Store 帐户。

如果您想让您妻子的 iPhone 看到相同的 CloudKit 数据,您必须退出,然后在 iCloud 设置中使用您的 Apple ID 重新登录。