关于Objective C:与Couldkit查询有关的问题并在CKReference的字段上使用谓词

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

我有2种记录类型:

  • 朋友:包含字段

    • url_of_profil_pic
    • 名称
    • ...
  • 投票:包含字段

    • date_of_vote
    • target_of_the_vote(朋友CK参考)
    • the_one_who_vote(cloudkit用户标识)
  • 我只是在徘徊如何在投票表上查询时获得url_of_profil_pic

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

    1
    2
    3
    4
    5
    6
    7
    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的谓词。

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


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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    //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建议在CloudKit上将超过1MB的任何内容保存为CKAsset(docs)。