0

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

我有 2 种记录类型:

  1. Friends : 包含字段
    • url_of_profile_pic
    • 姓名
    • ...
  2. 投票:包含字段
    • date_of_vote
    • target_of_the_vote(朋友 CK 参考)
    • 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"];
4

1 回答 1

0

假设您已获取记录,即url_of_profil_pica CKAsset,您必须将资产 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的内容保存为CKAssetCloudKit ( docs )。

于 2018-11-13T21:55:04.017 回答