Couldkit查询出现问题,并在CKReference字段上使用谓词

时间:2018-10-29 15:03:38

标签: objective-c nspredicate cloudkit ckquery ckreference

我对CKQuery的工作原理有一个基本的误解。

我有2种记录类型:

  1. 朋友:包含字段
    • url_of_profil_pic
    • 名称
    • ...
  2. 投票:包含字段
    • date_of_vote
    • target_of_the_vote(好友CKReference)
    • the_one_who_vote(cloudkit用户ID)

我只是在徘徊如何查询投票

上的 url_of_profil_pic

基本上,想要这样的东西:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"the_one_who_vote = %@", recordReference.recordID];
        CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Votes" predicate:predicate];
        query.sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc]initWithKey:@"createdAt" ascending:false]];
        CKQueryOperation *operation = [[[CKQueryOperation alloc] initWithQuery:query] autorelease];
        operation.desiredKeys = @[@"target_of_the_vote . url_of_profil_pic"];
        operation.resultsLimit = 10;
        operation.recordFetchedBlock = ^(CKRecord * _Nonnull record) 

此行将是给我URL的谓词。

operation.desiredKeys = @[@"target_of_the_vote . url_of_profil_pic"];

1 个答案:

答案 0 :(得分:0)

假设您已获取记录,并且url_of_profil_picCKAsset,则必须将资产URL转换为Data对象,然后才能将其保存到这样的文件中:

//record is the CKRecord you fetched
if let asset = record["url_of_profil_pic"] as? CKAsset{
  do{
    try yourData = Data(contentsOf: asset.fileURL)
    //Save yourData to disk...
  }catch{
    print("Error saving profile pic from CKAsset")
  }
}

Apple建议将超过 1MB 的内容保存为CKAsset在CloudKit(docs)上。