我正在尝试使用CloudKit同步和本地CoreData完成一个应用程序。大多数操作都能按预期工作,但是我找不到用于确定CloudKit报告的更改类型的方法。我得到了更改的记录,但是我需要知道更改是编辑,新记录还是删除。任何指导将不胜感激。
这是我认为可以配置为识别我需要对CoreData进行编辑的类型的代码。 Xcode 10.2.1 iOS 12.2 Swift(最新)
func fetchZoneChangesInZones( _ zones : [CKRecordZone.ID], _ completionHandler: @escaping (Error?) -> Void) {
var fetchConfigurations = [CKRecordZone.ID : CKFetchRecordZoneChangesOperation.ZoneConfiguration]()
for zone in zones {
if let changeToken = UserDefaults.standard.zoneChangeToken(forZone: zone) {
let configuration = CKFetchRecordZoneChangesOperation.ZoneConfiguration(previousServerChangeToken: changeToken, resultsLimit: nil, desiredKeys: nil)
fetchConfigurations[zone] = configuration
}//if let changeToken
}//for in
let operation = CKFetchRecordZoneChangesOperation(recordZoneIDs: zones, configurationsByRecordZoneID: fetchConfigurations)
operation.fetchAllChanges = true
var changedPatients = [CKRecord]()
var changedCategory1s = [CKRecord]()
//I thought that I should be able to query for the change type here and make separate arrays for each change type
operation.recordChangedBlock = { record in
if record.recordType == "Patient" {
changedPatients.append(record)
}
}//recordChangedBlock
operation.fetchRecordZoneChangesCompletionBlock = { [weak self] error in
for record in changedPatients {
//my actions here - need to choose new, changed or delete
self!.saveCKRecordToCoreData(record: record)
}//for record in
completionHandler(error)
}//fetchRecordZoneChangesCompletionBlock
operation.recordZoneFetchCompletionBlock = { recordZone, changeToken, data, moreComing, error in
UserDefaults.standard.set(changeToken, forZone: recordZone)
}//recordZoneFetchCompletionBlock
privateDatabase.add(operation)
}//fetchZoneChangesInZones
答案 0 :(得分:1)
我的敏捷性不是很好,但是我将在目标c中发布,以便您可以将其转换为敏捷性
首先,如果您想通知记录是否已被编辑,删除或创建,则需要注册推送通知。
然后订阅更新以在didFinishLaunchingWithOptions
- (void)subscribeToEventChanges
{
BOOL isSubscribed = [[NSUserDefaults standardUserDefaults] boolForKey:@"subscribedToUpdates"];
if (isSubscribed == NO) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"];
CKQuerySubscription *subscription = [[CKQuerySubscription alloc] initWithRecordType:@"Patient" predicate:predicate options:CKQuerySubscriptionOptionsFiresOnRecordCreation | CKQueryNotificationReasonRecordDeleted | CKQueryNotificationReasonRecordUpdated];
CKNotificationInfo *CKNotification=[[CKNotificationInfo alloc]init];
CKNotification.shouldSendContentAvailable=YES;
CKNotification.soundName=@"";
subscription.notificationInfo=CKNotification;
CKDatabase *publicDatabase = [[CKContainer containerWithIdentifier:@"your container identifir"] privateCloudDatabase];
[publicDatabase saveSubscription:subscription completionHandler:^(CKSubscription * _Nullable subscription, NSError * _Nullable error) {
if (error) {
// Handle here the error
} else {
// Save that we have subscribed successfully to keep track and avoid trying to subscribe again
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"subscribedToUpdates"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}];
}
}
didReceiveRemoteNotification
上得到通知这是一段代码
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
CKNotification *cloudKitNotification = [CKNotification notificationFromRemoteNotificationDictionary:userInfo];
if (cloudKitNotification.notificationType == CKNotificationTypeQuery) {
CKQueryNotification *queryNotification = (CKQueryNotification *)cloudKitNotification;
if (queryNotification.queryNotificationReason == CKQueryNotificationReasonRecordDeleted) {
// If the record has been deleted in CloudKit then delete the local copy here
} else {
// If the record has been created or changed, we fetch the data from CloudKit
CKDatabase *database;
if (queryNotification.databaseScope) {
database = [[CKContainer containerWithIdentifier:@"your container identifier"] privateCloudDatabase];
}
[database fetchRecordWithID:queryNotification.recordID completionHandler:^(CKRecord * _Nullable record, NSError * _Nullable error) {
if (error) {
// Handle the error here
} else {
if (queryNotification.queryNotificationReason == CKQueryNotificationReasonRecordUpdated) {
// Use the information in the record object to modify your local data
}else{
// Use the information in the record object to create a new local object
}
}
}];
}
}
}
答案 1 :(得分:0)
该解决方案是有关所使用操作版本的单独方法。我已经收到了通知,只是无法确定它们是更新,创建还是删除。只需在核心数据中搜索recordName(这是一个UUID)就可以处理更新和创建。如果找到,则编辑,如果未创建,则编辑。问题是删除-使用fetchRecordZoneChangesCompletionBlock无法识别删除。但是,操作族只有一种报告删除的方法-operation.recordWithIDWasDeletedBlock。我修改了以前的代码,并添加了删除代码,如下所示。
我的单个数据库订阅涵盖了整个私有数据库,因此不必订阅每种记录类型。
operation.fetchRecordZoneChangesCompletionBlock = { error in
for record in changedPatients {
//search for the record in coredata
if self.isSingleCoreDataRecord(ckRecord: record) {
//if found - then modify
self.saveUpdatedCloudKitRecordToCoreData(record: record)
} else {
//else add new
self.saveCKRecordToCoreData(record: record)
}
}//for record in
completionHandler(error)
}//fetchRecordZoneChangesCompletionBlock
operation.recordWithIDWasDeletedBlock = { (recordID, recordType) in
//delete the core data record here
let ckRecordToDelete = CKRecord(recordType: recordType, recordID: recordID)
self.removeOnePatientRecordFromCoreData(ckRecord: ckRecordToDelete)
}//recordWithIDWasDeletedBlock