将从Cloudkit获取的数据从一个viewcontroller传递到另一个

时间:2015-02-05 17:31:58

标签: ios objective-c cloudkit

我有一个调用此函数的按钮,并且还调用performSegueWithIdentifier。 Segue发生在数据是两个查询返回数据之前。我如何绕过这个?

-(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];

}];

} 强调文字

1 个答案:

答案 0 :(得分:0)

你必须更换你的segue并把它放在你的第二个完成块内。 因为否则你的完成块将在你已经推动了segue之后被调用。

[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);
                //Put it in here
                [self performSegueWithIdentifier:@"Recommend" sender:nil];
            } 
        }];

此外,您可以使用prepareForSegue将您的值传递给第二个控制器:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Set Segue name from storyboard
    if ([[segue identifier] isEqualToString:@"seguename"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}

Link to segue-answer