MBProgressHUD改变数据输出?

时间:2010-12-01 23:25:34

标签: iphone core-data ios-simulator

我正在尝试使用MBProgressHUD来显示加载动画,同时我正在访问信息以平滑从UITableView列表转到具有所选项目的详细信息的UIWebView的过程。如果我打电话给[self fetchPlayer];没有使用MBProgressHUD我没有问题,一切正常,虽然没有动画。但如果我打电话给[self loadingAnimation];它一半有效。我第一次从UITableView中选择它时,每次都会正确加载,但是如果我返回并选择相同或不同的项目,我将经常获得playerDetails项目的空值。我不确定MBProgressHUD方法可以做什么来导致这个,但这里是代码。 NSLog显示正确的信息 - 但调用这两种方法的主类不能正确输出它。

- (void)loadingAnimation {
 // The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];

    // Add HUD to screen
    [self.navigationController.view addSubview:HUD];

    HUD.labelText = @"Loading";

    // Show the HUD while the provided method executes in a new thread
 [HUD showWhileExecuting:@selector(fetchPlayer) onTarget:self withObject:nil animated:YES];
}


- (void)fetchPlayer {
 NSManagedObjectContext *pcontext = [[AppController sharedAppController] managedObjectContext];

 NSFetchRequest *pfetch = [[NSFetchRequest alloc] init];

 NSEntityDescription *pentity = [NSEntityDescription entityForName:@"Players"
              inManagedObjectContext:pcontext];
 [pfetch setEntity:pentity];

 NSPredicate *ppredicate = [NSPredicate predicateWithFormat:@"playerID=%@", [playerNews playerID]];
 [pfetch setPredicate:ppredicate];

 NSError *perror;
 NSArray *plist = [pcontext executeFetchRequest:pfetch error:&perror];

 playerDetails = [plist objectAtIndex:0];

 NSLog(@"%@ %@ %@ %@ %@", [playerNews playerID], 
        [playerDetails valueForKey:@"playerFirstName"],
        [playerDetails valueForKey:@"playerLastName"],
        [playerDetails valueForKey:@"position"],
        [playerDetails valueForKey:@"dateOfBirth"]);

 [pfetch release];

 NSManagedObjectContext *context = [[AppController sharedAppController] managedObjectContext];

 NSFetchRequest *fetch = [[NSFetchRequest alloc] init];

 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Teams"
             inManagedObjectContext:context];
 [fetch setEntity:entity];

 if (![playerDetails valueForKey:@"team"]) {
  team = [playerDetail team];
 } else {
  team = [playerDetails valueForKey:@"team"];

 }
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"team=%@", team];

 [fetch setPredicate:predicate];

 NSError *error;
 NSArray *list = [context executeFetchRequest:fetch error:&error];

 playerTeam = [list objectAtIndex:0];

 [fetch release];
}

1 个答案:

答案 0 :(得分:1)

这似乎至少是线程问题。您正在跨多个线程共享您的上下文,因为fetchPlayer是由MBProgressHUD在后台线程中执行的。

在后台线程中创建一个新的Context,并确保不跨线程共享NSManagedObjects。