2010-12-01 91 views
0

我試圖使用MBProgressHUD來顯示加載動畫,而我正在訪問的信息,以平滑從UITableView列表到具有所選項目的詳細信息的UIWebView的過程。如果我打電話[self fetchPlayer];沒有使用MBProgressHUD我沒有問題,一切工作正常,雖然沒有動畫。但是如果我打電話給[自動加載動畫];它一半工作。我第一次從UITableView中選擇一些東西,它每次都會正確加載,但如果我回去選擇相同或不同的項目,我會經常爲playerDetails項目獲取空值。我不確定MBProgressHUD方法可以做什麼來引起這一點,但這裏是代碼。 NSLog顯示正確的信息 - 但調用這兩種方法的主類不能正確輸出。MBProgressHUD更改數據輸出?

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

這似乎至少是線程問題。您正在多個線程中共享您的上下文,因爲fetchPlayer是由MBProgressHUD在後臺線程中執行的。

在後臺線程中創建一個新的上下文,並確保NSManagedObjects不在線程間共享。