使用recordName获取单个记录,它应该从icloud返回特定字段

时间:2015-10-29 11:31:30

标签: objective-c cloudkit

我想使用cloudkit从icloud获取单个记录。

这是我正在使用的代码:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recordName contains %@",recordId];                                       
NSLog(@"predicate------%@",predicate);
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"AmuzItems" predicate:predicate];
CKQueryOperation *queryOperation = [[CKQueryOperation alloc] initWithQuery:query];
queryOperation.database = publicDatabase;
queryOperation.resultsLimit =1 ;
queryOperation.desiredKeys = @[@"name",@"city",@"state"];
queryOperation.recordFetchedBlock = ^(CKRecord * record) {
        NSLog(@"record------%@",record);
};
queryOperation.queryCompletionBlock = ^(CKQueryCursor * cursor, NSError * operationError) {
    if (operationError) {                                                                                              NSLog(@"error--------%@",operationError);                                                                                                                                            
 } else
    {
        NSLog(@"cursor------%@",cursor);
    }
};

只想检索name, city, state字段。 RecordType.code中有15个字段不返回任何内容,甚至没有返回错误。

1 个答案:

答案 0 :(得分:3)

对于像我这样面临同样问题的人:

CKDatabase *publicDatabase = [[CKContainer defaultContainer] publicCloudDatabase];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recordID == %@",[[CKRecordID alloc]initWithRecordName:recordId]];
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"RecordTypeName" predicate:predicate];
CKQueryOperation *queryOperation = [[CKQueryOperation alloc] initWithQuery:query];
NSOperationQueue * operationQueue = [[NSOperationQueue alloc] init];
queryOperation.database = publicDatabase;
queryOperation.resultsLimit =1 ;
queryOperation.desiredKeys = @[@"name",@"city",@"state"];
queryOperation.recordFetchedBlock = ^(CKRecord * record) {
 NSLog(@"record------%@",record);
};
queryOperation.queryCompletionBlock = ^(CKQueryCursor * cursor, NSError * error) {
    if (error) {
          NSLog(@"error--------%@",error);            
    } else
    {
        if (cursor) {
            NSLog(@"cursor-------%@",cursor);
        }
        else
        {
                NSLog(@"end");
        }
    }
};
 [operationQueue addOperation:queryOperation];