1

首先,这一切都可以在不使用 GCD 的情况下工作,但我希望这发生在一个单独的线程中,所以尝试 GCD。我有一个登录屏幕,在按下登录按钮时我有以下操作:

- (void)login
{
    dispatch_queue_t buckyballLoginFetcherQ = dispatch_queue_create("Login Queue", NULL);
    dispatch_async(buckyballLoginFetcherQ, ^
    {
        NSDictionary *resultDictionary = [MyService login:self.name.text password:self.password.text];
        self.userDetails = [resultDictionary valueForKey:USER_DETAILS_ATTRIBUTE];
        [self performSegueWithIdentifier:@"Login" sender:self];
    });
}

在上面调用的 MyService 方法中:

+ (NSDictionary *)executeRequest:(NSDictionary *)requestDictionary
{
    // Prepare the URL request and do the following
    NSData *results = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&urlRequestError];

    // Process results
    ...
}

现在崩溃的位:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Login"])
{
    MyDestinationTableViewController *myDestinationTableViewController = nil;
    UITabBarController *tbc = (UITabBarController *)[segue destinationViewController];
    for (UIViewController *vc in [tbc viewControllers])
    {
        if ([vc isKindOfClass:[UINavigationController class]])
        { // in our case all view controlers are navigation controllers :-)
            UINavigationController *nc = (UINavigationController *)vc;
            if ([[[nc viewControllers] lastObject] isKindOfClass:[BuckyballsTableViewController class]])
            {
                myDestinationTableViewController = [[nc viewControllers] lastObject];

                /**************CRASH LINE************/
                buckyballsTableViewController.userDetails = self.userDetails;
            }
        }
    }
}

再次没有 GCD 它可以工作,但它会支撑屏幕,所以我想异步执行它。是导致问题的实例成员吗?或者我需要以不同的方式使用它还是用它做更多的事情?谢谢...

4

1 回答 1

0

只有主线程可以操作 UI,因此使用此代码片段在主线程上调用这些位:

dispatch_async(dispatch_get_main_queue(), ^{
    [self performSegueWithIdentifier:@"Login" sender:self];
});
于 2012-12-12T19:08:37.013 回答