Home > Blockchain >  Delay in passing data using segue Objective-C
Delay in passing data using segue Objective-C

Time:11-29

I'm trying to pass the storeId to another viewController using segue, however, there seems to be a delay when passing data? Data is "nil" at first, I have to tap it again in order it to pass the correct data.

Here is my code


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
    self.passStoreData = self.storeList [indexPath.row];
    self.passStoreId = [self.storeList [indexPath.row] valueForKey:@"storeId"];
    self.passStoreName = [self.storeList [indexPath.row] valueForKey:@"storeName"];
    NSString *store = [NSString stringWithFormat:@"%@ : %@", self.passStoreId, self.passStoreName];
    
    [self.storeButton setTitle:[NSString stringWithFormat:@"%@", store] forState:UIControlStateNormal];
    storeTable.hidden = YES;
    
    [self performSegueWithIdentifier:@"showOrder" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    OrderListViewController *orderListVc = [segue destinationViewController];

    orderListVc.theStoreId = self.passStoreData;
    
}

What can I do to avoid the delay in passing the data using segue? Thank you.

CodePudding user response:

By the time you call [segue destinationViewController], the OrderListViewController is loaded and ready to use. So any code in viewDidLoad will already have been called, way before you set theStoreId.

Move your view setup code to viewWillAppear: and it should work.

CodePudding user response:

After trying and researching. I found the answer to this.

I used dispatch_async(dispatch_get_main_queue() to run it on main thread and the passing of data through segue was not delayed. Thank you

  • Related