I have a button that calls this function and also calls performSegueWithIdentifier. Segue occurs before the data is the two query returns data. How do i get round this?
-(void)recommendSomeone {
    NSString *currentUserID = [NSString stringWithFormat:@"%@%@",@"KU",self.liaResponse[@"id"]];
    CKContainer *myContainer  = [CKContainer defaultContainer];
    CKDatabase *publicDatabase = [myContainer publicCloudDatabase];
    CKRecordID *currentUserRecordID = [[CKRecordID alloc] initWithRecordName:currentUserID];
    CKReference *recordToMatch = [[CKReference alloc] initWithRecordID:currentUserRecordID action:CKReferenceActionNone];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"contactList CONTAINS %@",recordToMatch];
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"knotworkGhostUsers" predicate:predicate];
[publicDatabase performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
    if (error) {
        // Error handling for failed fetch from public database
        NSLog(@"error querying knotwork users %@", error);
    }
    else {
        // Display the fetched records
        self.randomGhostUser = results[arc4random_uniform([results count])];
        NSLog(@"this is the first name %@", self.randomGhostUser[@"firstname"]);
        NSLog(@"knotwork ghost users for current user data returned ordinary title %@",results);
        NSString* searchQuery = @" ";
        NSString *kuTitle = [self.randomGhostUser[@"title"] lowercaseString];
        NSArray *keyWords = @[@"developer",@"networking",@"sales manager",@"engineer",@"doctor",@"facility manager"];
        for(NSString* keyWord in keyWords){
            NSRange checkResult = [kuTitle rangeOfString:keyWord];
            if(checkResult.location != NSNotFound){
                searchQuery = [NSString stringWithFormat:@"%@%@%@",searchQuery,@" ",keyWord];
            }
        }
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"allTokens tokenmatches[cdl] %@ AND contactList CONTAINS %@",searchQuery,recordToMatch];
        CKQuery *query2 = [[CKQuery alloc] initWithRecordType:@"knotworkGhostUsers" predicate:predicate];
        [publicDatabase performQuery:query2 inZoneWithID:nil completionHandler:^(NSArray *response, NSError *error) {
            if (error) {
                // Error handling for failed fetch from public database
                NSLog(@"error querying knotwork users %@", error);
            }
            else {
                // Display the fetched records
                self.recommendedGhostUser = response;
                NSLog(@"knotwork users data returned after recom %@", self.recommendedGhostUser);
            } 
        }];
    }
    [self performSegueWithIdentifier:@"Recommend" sender:nil];
}];
} emphasized text
 
     
    