在私有数据库中为自定义区域创建订阅失败
Create subscription for custom zone in private database fails
我在私有数据库中成功创建了自定义区域,然后尝试为该自定义区域创建订阅,但订阅创建失败并出现错误:"An error indicating that some items failed, but the operation succeeded overall."。
ios 文档建议使用 CKRecordZoneSubscription 创建区域订阅。但我仍然遇到错误。我该如何解决这个问题?
var ContactZonePrivateDb: CKRecordZone = CKRecordZone(zoneName: Cloud.PrivateZone.Contact.ZoneName)
func createCustomContactInfoZone (completion: @escaping (Error?) -> Void){
let status = UserDefaults.standard.bool(forKey: Cloud.PrivateDB.CustomZoneCreated)
if status == true {
return
}
let createZoneGroup = DispatchGroup()
createZoneGroup.enter()
let createZoneOperation = CKModifyRecordZonesOperation(recordZonesToSave: [ContactZonePrivateDb], recordZoneIDsToDelete: [] )
createZoneOperation.modifyRecordZonesCompletionBlock = { (saved, deleted, error) in
if error != nil {
if let ckerror = error as? CKError {
self.aErrorHandler.handleCkError(ckerror: ckerror)
}
completion(error)
}
UserDefaults.standard.set(true, forKey: Cloud.PrivateDB.CustomZoneCreated)
self.subscribePrivateZoneContact()
completion(nil)
}
createZoneGroup.leave()
createZoneOperation.qualityOfService = .userInitiated
self.privateDB?.add(createZoneOperation)
}
func subscribePrivateZoneContact() {
let status = UserDefaults.standard.bool(forKey: Cloud.PrivateZone.Contact.SubscriptionID)
if (status == true) {
return
}
let subscriptionZone = CKRecordZoneSubscription(zoneID: ContactZonePrivateDb.zoneID)
let operation = CKModifySubscriptionsOperation(subscriptionsToSave: [subscriptionZone], subscriptionIDsToDelete: nil)
operation.modifySubscriptionsCompletionBlock = { saved, deleted, error in
guard error == nil else {
if let ckerror = error as? CKError {
self.aErrorHandler.handleCkError(ckerror: ckerror)
}
return
}
UserDefaults.standard.set(true, forKey: Cloud.PrivateZone.Contact.SubscriptionID)
DispatchQueue.main.async {
print("Successfully added Private zone subscription.\(self.ContactZonePrivateDb.zoneID)")
}
}
operation.qualityOfService = .userInitiated
self.privateDB?.add(operation)
}
如果您在 CloudKit 控制面板中删除您的订阅,然后 运行 您的订阅创建代码一次,它是否仍然显示错误?
我问的原因是因为我相信如果您尝试重新创建同名订阅,您会看到该错误。这样做没问题,错误可以忽略。
发现要在自定义区域上创建订阅,您必须先在自定义区域中创建一条记录。但其他订阅则不然。不知道为什么?
我在私有数据库中成功创建了自定义区域,然后尝试为该自定义区域创建订阅,但订阅创建失败并出现错误:"An error indicating that some items failed, but the operation succeeded overall."。
ios 文档建议使用 CKRecordZoneSubscription 创建区域订阅。但我仍然遇到错误。我该如何解决这个问题?
var ContactZonePrivateDb: CKRecordZone = CKRecordZone(zoneName: Cloud.PrivateZone.Contact.ZoneName)
func createCustomContactInfoZone (completion: @escaping (Error?) -> Void){
let status = UserDefaults.standard.bool(forKey: Cloud.PrivateDB.CustomZoneCreated)
if status == true {
return
}
let createZoneGroup = DispatchGroup()
createZoneGroup.enter()
let createZoneOperation = CKModifyRecordZonesOperation(recordZonesToSave: [ContactZonePrivateDb], recordZoneIDsToDelete: [] )
createZoneOperation.modifyRecordZonesCompletionBlock = { (saved, deleted, error) in
if error != nil {
if let ckerror = error as? CKError {
self.aErrorHandler.handleCkError(ckerror: ckerror)
}
completion(error)
}
UserDefaults.standard.set(true, forKey: Cloud.PrivateDB.CustomZoneCreated)
self.subscribePrivateZoneContact()
completion(nil)
}
createZoneGroup.leave()
createZoneOperation.qualityOfService = .userInitiated
self.privateDB?.add(createZoneOperation)
}
func subscribePrivateZoneContact() {
let status = UserDefaults.standard.bool(forKey: Cloud.PrivateZone.Contact.SubscriptionID)
if (status == true) {
return
}
let subscriptionZone = CKRecordZoneSubscription(zoneID: ContactZonePrivateDb.zoneID)
let operation = CKModifySubscriptionsOperation(subscriptionsToSave: [subscriptionZone], subscriptionIDsToDelete: nil)
operation.modifySubscriptionsCompletionBlock = { saved, deleted, error in
guard error == nil else {
if let ckerror = error as? CKError {
self.aErrorHandler.handleCkError(ckerror: ckerror)
}
return
}
UserDefaults.standard.set(true, forKey: Cloud.PrivateZone.Contact.SubscriptionID)
DispatchQueue.main.async {
print("Successfully added Private zone subscription.\(self.ContactZonePrivateDb.zoneID)")
}
}
operation.qualityOfService = .userInitiated
self.privateDB?.add(operation)
}
如果您在 CloudKit 控制面板中删除您的订阅,然后 运行 您的订阅创建代码一次,它是否仍然显示错误?
我问的原因是因为我相信如果您尝试重新创建同名订阅,您会看到该错误。这样做没问题,错误可以忽略。
发现要在自定义区域上创建订阅,您必须先在自定义区域中创建一条记录。但其他订阅则不然。不知道为什么?