CloudKit 处理不同 iCloud 帐户的应用程序

CloudKit App to handle different iCloud accounts

我有一个使用 CloudKit 和 iCloud 将用户数据保存在私人数据库中的应用程序。我已经编写了代码来处理用户通过 Settings -> iCloud 在其设备上注销 iCloud 并以其他用户身份重新登录的情况。我目前处于 Beta 测试模式,当我作为内部测试人员尝试此模式时,应用程序会在我更改帐户时崩溃。当我使用 Xcode 模拟器或真实设备在开发模式下对此进行测试时,我的代码运行良好。我的问题是:当设备上的 iCloud 帐户更改时,CloudKit 应用程序是否应该能够处理?如果是这样,这种情况是否可以通过 TestFlight 进行内部测试。我处理帐户更改的代码如下...

func isCloudAccountAvailableASync() {
  container.accountStatusWithCompletionHandler { (accountStatus, error) in

    switch accountStatus {
    case .Available:
      print("INFO: iCloud Available!")
      // begin sync process by finding the current iCloud user
      self.fetchUserID()
      return
    case .NoAccount:
      print("INFO: No iCloud account")
    case .Restricted:
    print("WARNING: iCloud restricted")
    case .CouldNotDetermine:
      print("WARNING: Unable to determine iCloud status")
    }

    // if you get here, no sync happened so make sure to exec the callback
    self.syncEnded(false)
  }
}


func fetchUserID(numTries: Int = 0) {

  container.fetchUserRecordIDWithCompletionHandler( { recordID, error in

    if let error = error {

      // error handling code

      // reach here then fetchUser was a failure
      self.syncEnded(false)
    }
    else {

      // fetchUserID success

      if self.lastiCloudUser == nil {
        print("INFO: our first iCloud user! \(recordID)")
        self.saveUserID()
      }
      else if !recordID!.isEqual(self.lastiCloudUser) {
        // User has changed!!!!!!!!!!!!!
        self.saveUserID()

        // delete all local data

        // reset the saved server token
        self.saveServerToken(nil)
        // try again with reset fields
      }
      // else user never changed, then just create a zone

      // continue the sync process
      self.initZone()
    }
  })
}

---EDIT/UPDATE:--- 这是我的崩溃日志的截图

---EDIT/UPDATE 2:--- 我能够使用不同的崩溃日志生成另一个崩溃。这个崩溃日志仍然没有指向我的代码,但至少描述了一个函数...

很难说您的应用是否完全崩溃了。 您必须知道,在更改帐户后,您应该重置应用程序的状态(清除本地用户数据并离开具有用户特定数据的屏幕) 在您的应用程序启动时,您应该调用这样的函数:

func reactToiCloudloginChanges() {
    NotificationCenter.default.addObserver(forName: NSNotification.Name.NSUbiquityIdentityDidChange, object: nil, queue: nil) { _ in
        // The user’s iCloud login changed: should refresh all local user data here.
        Async.main {
            self.viewController?.removeFromParentViewController()
            // Or some other code to go back to your start screen.
        }
        return
    }
}

我一直让它崩溃,不知何故得到了一个崩溃日志,其中包含一行代码,我可以 link 到我的 Xcode 项目中的一行。我的问题是 NSOrderedSet 和 iOS 9. That issue can be found here。我不知道为什么我以前的崩溃日志中只有十六进制,如果有人知道如何处理十六进制崩溃日志我很想听听。

以下是我的原始问题的答案:

  1. 当设备上的 iCloud 帐户更改时,CloudKit 应用程序应该能够处理吗? 回答:是
  2. 如果是这样,这个案例是否可以通过 TestFlight 进行内部测试? 回答:是