2012-03-12 77 views
0

我很抱歉在這個漫長的問題的開始。ios將數據傳遞給MBProgressHUD以奇怪的崩潰結尾

在我的應用程序中,我下載了大視頻文件(30-60Mb)。我顯然想告訴用戶有關進度。下載來自Urban Airship,我使用他們的一種方法來獲取進度。但是,這發生在TableViewController中,但下載指示器(MBProgressHUD)從另一個視圖開始,將其稱爲UADetail。

爲了將進度從一個視圖傳遞給另一個視圖,我使用了一個Singleton。隨機地,它可能發生的早,晚或完全沒有,應用程序在日誌中崩潰;

2012-03-12 15:13:30.528 isengua烯[3478:681F] HUD lessonDownloadProgress:0.053478 2012-03-12 15:13:30.553 isengua烯[3478:707] Lessonlist進展: 0.055272 isengua-en [3478:707] LLVC downHUD進度:0.055272 2012-03-12 15:13:30.565 isengua-en [3478:707] - [LessonListViewController> productsDownloadProgress: [CFNumber _getValue:forType:]:發送到的消息數量:[行57] [StoreFrontDelegate] productsDownloadProgress:0.055272 count:1 2012-03-12 15:13:30.569 isengua-en [3478:6307] * - 解除分配實例0x83c6e80

首先是LessonListViewController;

- (void)productsDownloadProgress:(float)progress count:(int)count 
{ 
    DataManager *sharedManager = [DataManager sharedManager]; 
    sharedManager.downHUD = [NSNumber numberWithFloat:progress]; 
    NSLog(@"Lessonlist progress: %f", progress); 
    NSLog(@"LLVC downHUD progress: %f", [sharedManager.downHUD floatValue]); 
    UALOG(@"[StoreFrontDelegate] productsDownloadProgress: %f count: %d", progress, count); 
    if (count == 0) { 
     NSLog(@"Downloads complete in LessonListView!"); 
    } 

}

的辛格爾頓看起來是這樣的;

@implementation DataManager 

@synthesize downHUD; 




+ (DataManager *)sharedManager 
{ 
    static DataManager *sharedManager = nil; 
    static dispatch_once_t predicate; 
    dispatch_once(&predicate, ^{ 
     sharedManager = [[self alloc] init]; 
    }); 
    return sharedManager; 

} 

- (id)init { 
    if (self = [super init]) { 
     downHUD = [NSNumber numberWithFloat:(float)0]; 
    } 
    return self; 
} 


- (void)dealloc { 
// Should never be called, but just here for clarity really. 
    NSLog(@"dealloc called in DataManager"); 
} 



@end 

它然後在UADetail得到讀;

- (void)showWithLabelDeterminate { 

    HUD = [[MBProgressHUD alloc] initWithView:self.view]; 
    [self.view addSubview:HUD]; 


    // Set determinate mode 
    HUD.mode = MBProgressHUDModeIndeterminate; 
    HUD.delegate = self; 
    HUD.labelText = NSLocalizedString(@"Waiting",""); 

    // myProgressTask uses the HUD instance to update progress 
    [HUD showWhileExecuting:@selector(lessonDownloadProgress) onTarget:self withObject:nil animated:YES]; 
} 


-(void)lessonDownloadProgress 
{ 
    DataManager *sharedManager = [DataManager sharedManager]; 
    HUD.mode = MBProgressHUDModeDeterminate; 
    HUD.progress = [sharedManager.downHUD floatValue]; 
    HUD.labelText = NSLocalizedString(@"DownLoading",""); 

    while (HUD.progress < 1) 
    { 
     [self parentViewController]; 
     NSLog(@"HUD lessonDownloadProgress: %f", HUD.progress); 
     HUD.progress = [sharedManager.downHUD floatValue];      
     NSString *percent = [NSString stringWithFormat:@"%.0f", HUD.progress/1*100]; 
     HUD.detailsLabelText = [percent stringByAppendingString:@"%"]; 
    } 
} 

回答

0

我想我自己找到了解決方案。 在while循環中放入一個usleep(50000)後,它停止。 也許循環運行速度過快,並且經常檢查sharedManager.downHUD,干擾從其他類寫入它。