简体   繁体   English

如何在CloudKit中设置对当前用户的引用?

[英]How to set the reference to the current user, in CloudKit?

I have a CKRecordType with a reference field that I want to set to the current user of the app. 我有一个CKRecordType其中包含我想设置为应用程序当前用户的参考字段。 在此处输入图片说明

CKRecord * record = [[CKRecord alloc] initWithRecordType:@"UserStatus"];
[record setValue:@"online" forKey:@"status"];
// what user should be?
[record setValue:user forKey:@"user"];

How should I do that? 我应该怎么做?

First, get the userId : 首先,获取userId

- (void)getUserId
{
    [self.defaultContainer fetchUserRecordIDWithCompletionHandler:^(CKRecordID * _Nullable recordID,
                                                                    NSError * _Nullable error)
     {
         if (error == nil)
         {
             self.userRecordID = recordID;
         }
     }];
}

Then: 然后:

CKRecord * record = [[CKRecord alloc] initWithRecordType:@"UserStatus"];
record[@"status"] = @"online";

CKReference *userReference = [[CKReference alloc] initWithRecordID:self.userRecordID action:CKReferenceActionDeleteSelf];
record[@"user"] = userReference;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM