在CloudKit中获取私人用户记录

时间:2015-09-29 00:21:26

标签: ios cloudkit

我正在尝试在CloudKit中获取用户保存的数据。我可以在CloudKit仪表板中看到该记录,但无法通过应用程序中的代码访问它。

{{1}}

这会获取用户记录信息,但不会显示已保存的私人记录的ID。我怎么能得到它们?

1 个答案:

答案 0 :(得分:0)

找出解决方案。您必须使用CKQuery和NSPredicate查询私人记录。

-(void)getUserRecordID {
    CKContainer *defaultContainer =[CKContainer defaultContainer];
    [defaultContainer fetchUserRecordIDWithCompletionHandler:^(CKRecordID *recordID, NSError *error) {
        if (!error) {
            self.userRecordID = recordID;
            NSLog(@"user record id: %@",recordID.recordName);
            [self getStatsRecord];
        }
        else {
            NSLog(@"error: %@",error.localizedDescription);
        }
    }];

}

-(void)getStatsRecord {
    CKDatabase *privateDatabase = [[CKContainer defaultContainer] privateCloudDatabase];
    CKReference *reference = [[CKReference alloc] initWithRecordID:self.userRecordID action:CKReferenceActionNone];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"creatorUserRecordID = %@", reference];
    CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Stats" predicate:predicate];
    [privateDatabase performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
        if (error) {
            NSLog(@"error: %@",error.localizedDescription);
        }
        else {
            if (![results firstObject]) {
                [self createBlankRecord];
            }
            else {
                CKRecord *record = [results firstObject];
                NSLog(@"%@",record);
            }
        }
    }];

}