I am building an application which uses CloudKit as backend. I have a ShoppingList record and GroceryItem record. A shopping list can have many grocery items. I am using the reverse relationship technique which means grocery items have a reference to the shopping list (parent).
Now, I want to display the number of grocery items in the shopping list. Here is my implementation:
-(void) getAllShoppingLists:(GetAllShoppingListsResult)getAllShoppingLists {
    CKQuery *query = [[CKQuery alloc] initWithRecordType:@"ShoppingLists" predicate:[NSPredicate predicateWithValue:YES]];
    [_privateDB performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
        for(CKRecord *record in results) {
            ShoppingList *shoppingList = [[ShoppingList alloc] initWithRecord:record];
            [self getItemsByShoppingList:shoppingList result:^(NSArray *results, NSError *error) {
                shoppingList.noOfGroceryItems = results.count;
            }];
        }
        **getAllShoppingLists(results,error);** THIS RETURNS WITHOUT THE noOfGroceryItems BEING UPDATED
    }];
}
What can I do to solve this issue? Is there a better way using CKQuery to simple get the total number of grocery items instead of running a for each on each single Shopping List?
 
     
     
    