ios将数据传递给MBProgressHUD以奇怪的崩溃结束

时间:2012-03-12 15:49:24

标签: ios urbanairship.com mbprogresshud

我为这个冗长的问题在一开始就道歉。

在我的应用程序中,我下载了大视频文件(30-60Mb)。我显然想告诉用户进度。下载来自Urban Airship,我使用他们的一种方法来获得进展。然而,这发生在TableViewController中,但下载指示符(MBProgressHUD)从另一个视图开始,称之为UADetail。

要将进度从一个视图传递到另一个视图,我使用Singleton。随机地,它可以发生在早期,晚期或根本不发生,应用程序在日志中崩溃;

  

2012-03-12 15:13:30.528 isengua-en [3478:681f] HUD课程下载进度:0.053478   2012-03-12 15:13:30.553 isengua-en [3478:707]课程列表进度:0.055272   2012-03-12 15:13:30.562 isengua-en [3478:707] LLVC downHUD进展:0.055272   2012-03-12 15:13:30.565 isengua-en [3478:707] - [LessonListViewController> productsDownloadProgress:count:] [Line 57] [StoreFrontDelegate] productsDownloadProgress:0.055272 count:1   2012-03-12 15:13:30.569 isengua-en [3478:6307] * - [CFNumber _getValue:forType:]:发送到解除分配的实例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!");
    }

}

Singleton看起来像这样;

@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:@"%"];
    }
}

1 个答案:

答案 0 :(得分:0)

我想我自己找到了解决方案。 在while循环中放入一个usleep(50000)后它停止了。 也许循环运行得太快并且经常检查sharedManager.downHUD,干扰从另一个类写入它。