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 に答える