无法共享CKRecord [英] Unable to share a CKRecord

查看:135
本文介绍了无法共享CKRecord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了下面的代码来共享CKRecord:

I wrote the code below to share a CKRecord:

CKRecordZone *restaurantsZone = [[CKRecordZone alloc] initWithZoneName:@"RestaurantsZone"];
CKRecordID *recordID = [[CKRecordID alloc] initWithRecordName:self.recordName zoneID:restaurantsZone.zoneID];
CKRecord *record = [[CKRecord alloc] initWithRecordType:@"Restaurant" recordID:recordID];
[record setValue:self.restaurant forKey:@"name"];

UICloudSharingController *cloudSharingController = [[UICloudSharingController alloc] initWithPreparationHandler:^(UICloudSharingController * _Nonnull controller, void (^ _Nonnull preparationCompletionHandler)(CKShare * _Nullable, CKContainer * _Nullable, NSError * _Nullable)) {
    [self shareRootRecord:record name:self.restaurant completion:preparationCompletionHandler];
}];
cloudSharingController.delegate = self;
[self presentViewController:cloudSharingController animated:YES completion:nil];

shareRootRecord 函数:

- (void)shareRootRecord:(CKRecord *)rootRecord name:(NSString *)name completion:(void (^)(CKShare * _Nullable share, CKContainer * _Nullable container, NSError * _Nullable error))completion
{
    CKShare *shareRecord = [[CKShare alloc] initWithRootRecord:rootRecord];
    shareRecord[CKShareTitleKey] = name;
    NSArray *recordsToSave = @[rootRecord, shareRecord];
    CKContainer *container = [CKContainer defaultContainer];
    CKDatabase *privateDatabase = [container sharedCloudDatabase];

    CKModifyRecordsOperation *operation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:recordsToSave recordIDsToDelete:@[]];
    [operation setPerRecordCompletionBlock:^(CKRecord * _Nullable record, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
        }
    }];
    [operation setModifyRecordsCompletionBlock:^(NSArray<CKRecord *> * _Nullable savedRecords, NSArray<CKRecordID *> * _Nullable deletedRecordIDs, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
        }
        completion(shareRecord, container, error);
    }];

    [privateDatabase addOperation:operation];
}

现在,当我运行此代码时,抛出以下错误:在共享数据库中只能访问共享区域。不过,我似乎无法找出原因。有任何想法吗?

Now, when I run this code, the following error is thrown: Only shared zones can be accessed in the shared DB. I can't seem to be able to figure out why, though. Any ideas?

推荐答案

确保要共享的 CKRecord 是在共享之前已经存在于所有者privateDB中。

Make sure the CKRecord you want to share is already in the owners privateDB before you share it.

当参与者接受共享时,记录将出现在参与者sharedDB中。

When a participant accepts the share, that's when the record will appear in the participants sharedDB.

此代码创建一条记录和一个共享,然后尝试使用记录修改所有者的sharedDB。

This code creates a record and a share and then tries to modify the sharedDB of the owner with the records.

从概念上讲,您想与参与者共享所有者的privateDB中的记录。参与者的sharedDB充当所有者所有者的privateDB的窗口。

Conceptually, you want to share a a record in an owners privateDB with a participant. The sharedDB of a participant acts as a window into the privateDB of the owner.

这篇关于无法共享CKRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆