gpt4 book ai didi

ios - 无法共享 CKRecord

转载 作者:行者123 更新时间:2023-11-29 00:07:04 24 4
gpt4 key购买 nike

我写了下面的代码来共享一个 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];
}

现在,当我运行此代码时,会引发以下错误:只能在共享数据库中访问共享区域。但我似乎无法弄清楚为什么。有什么想法吗?

最佳答案

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

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

此代码创建一个记录和一个共享,然后尝试使用这些记录修改所有者的共享数据库。

从概念上讲,您想要与参与者共享所有者私有(private)数据库中的记录。参与者的 sharedDB 充当进入所有者 privateDB 的窗口。

关于ios - 无法共享 CKRecord,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47703266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com