2015-02-05 197 views
0

我有一個調用此函數並調用performSegueWithIdentifier的按鈕。 Segue發生在數據是兩個查詢返回數據之前。我該如何解決這個問題?將從Cloudkit提取的數據從一個視圖控制器傳遞到另一個視圖控制器

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

}]; 

} 強調文本

+0

你似乎忘了問一個問題。 – 2015-02-05 17:39:00

+0

通常情況下,您將在prepareForSegue中傳遞數據。你有沒有試圖實現這一點?關於在控制器之間傳遞數據,SO上有數百個答案,您應該進行一些搜索。 – rdelmar 2015-02-05 17:44:08

+0

我試着將數據傳遞給prepareForSegue。但問題仍然是,在數據提取之前發生prepareForSegue – onyemmeme 2015-02-05 18:10:26

回答

0

您必須更換賽格瑞並把它放在你的第二個完成區塊內。 因爲否則您的完成塊將在您已經推入了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

+0

謝謝。我需要將第一個完成塊的結果和第二個結果的響應傳遞給我繼續使用的視圖控制器。我怎麼做?即使我已經將這兩個傳遞給了全局變量,塊之外的全局變量是空的 – onyemmeme 2015-02-05 19:22:59

相關問題