Trouble with Couldkit Query and using predicates on field of CKReference

77 Views Asked by At

i have a basic missunderstanding about how CKQuery works.

I have 2 Record types :

  1. Friends : containing fields
    • url_of_profil_pic
    • name
    • ...
  2. Vote : containing fields
    • date_of_vote
    • target_of_the_vote (Friends CKReference)
    • the_one_who_vote (cloudkit user id)

I am just wandering how I could get the url_of_profil_pic when i'm querying on Vote table

Basically, wanted smthing like this :

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) 

where this line will be the predicate that gives me the URL.

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

There are 1 best solutions below

1
On BEST ANSWER

Assuming you've fetched the record, and that url_of_profil_pic is a CKAsset, you have to convert the asset URL to a Data object that you can save to a file like this:

//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 recommends saving anything over 1MB as a CKAsset on CloudKit (docs).