在2个UITableViews应用程序冻结之间交替

时间:2012-03-25 14:13:04

标签: iphone objective-c ios uitableview mbprogresshud

我正在使用ViewController,委托& datasources 2个不同的tableviews。根据用户希望看到的内容(对于视图中的某些区域,使用'touchesBegan'进行切换。

viewcontroller是选项卡式应用程序的3个子控制器之一。

tableviews之间的交替只是fin。我得到了正确的数据,布局等,我可以随时随地进行更改。

第一个tableview不包含困难数据,所以它在几毫秒内加载。

第二个tableview2包含大约需要2-3秒加载的数据(取决于coredata中的实体数量)。而这个数据加载& tableview2重绘我显示MBProgressHUD。这也有效。

问题:如果我在加载& table2时与 tabbarcontroller 进行互动hud正在旋转,应用程序'冻结'hud运行无限长并且任何userinteractino都被禁用。以及点击的标签也不会打开。

代码:touchesBegan FUnction

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{


    if (isLoadingAllMonth) {
        return;
    }
    [[MBProgressHUD HUDForView:self.view]removeFromSuperview];


    UITouch *touch = [[event allTouches]anyObject];
    int viewTag = touch.view.tag;                           // 1 for thisMonth, 2 for allMonth


    if (viewTag == 1) {
        [allMonthButtonView setAlpha:.8];
        [thisMonthButtonView setAlpha:1];
        if (allMonthIsActive == NO) {
            return;
        }
        else{
            [self reloadThisMonth];
            [allMonthTable removeFromSuperview];
            [self.view addSubview:thisMonthTable];

            allMonthIsActive = NO;
        }
    }

    else if(viewTag == 2){

        if (!allMonthIsActive) {
            allMonthIsActive = YES;



            if (isLoadingAllMonth) {
                return;
            }

            [self.view addSubview:HUD];
            [allMonthTable setFrame:CGRectMake(4, 64, 312, 343)];
            [allMonthTable setBackgroundColor:[self grayColor]];
            [self.view addSubview:allMonthTable];

            HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
                isLoadingAllMonth = YES;

                [self reloadAllMonth];

                dispatch_async(dispatch_get_main_queue(), ^(void) {

                    [self.allMonthTable reloadData];            // Or reload tableView
                    [HUD hide:YES];
                });

            });
        }
    }

}

代码:reloadAllMonth

-(void)reloadAllMonth{
    UIFont *titleFont = [UIFont fontWithName:@"Cochin" size:14.0];
    UIFont *detailFont = [UIFont fontWithName:@"Cochin" size:18.0];
    [[MBProgressHUD HUDForView:self.view] setLabelFont:titleFont];
    [[MBProgressHUD HUDForView:self.view] setDetailsLabelFont:detailFont];

    [[MBProgressHUD HUDForView:self.view] setLabelText:@"Please wait"];


    [[MBProgressHUD HUDForView:self.view] setDetailsLabelText:@"Cleaning Cache"];
    if (allMonthData.count != 0) {
        for (NSMutableArray *arr in allMonthData) {
            [arr removeAllObjects];
        }

        [allMonthData removeAllObjects]; 

        for (NSMutableArray *arr in allMonthDataNumbers) {
            [arr removeAllObjects];
        }

        [allMonthDataNumbers removeAllObjects]; 

    }

    NSDate *rootDate = [NSDate dateWithTimeIntervalSinceReferenceDate:(10*365*24*60*60)];
    int rootMonth = [[dataHandler getMonthNumber:rootDate] intValue];

    NSMutableArray *allExp = [[NSMutableArray alloc]init];
    NSNumber *currentMonth = [dataHandler getMonthNumber:[NSDate date]];
    NSMutableArray *temp = [[NSMutableArray alloc]init ];
    NSNumber *tempMonth = [NSNumber numberWithInt:(currentMonth.intValue+1)];

    [temp removeAllObjects];
    [[MBProgressHUD HUDForView:self.view] setDetailsLabelText:@"Updating Data..."];

    while (tempMonth.intValue > rootMonth) {
        tempMonth = [NSNumber numberWithInt:(tempMonth.intValue-1)];
        temp = [NSMutableArray arrayWithArray:[dataHandler fetchAllExpensesForMonth:tempMonth]] ;
        if (temp.count != 0) {
            [allExp addObject:temp];
        }

    }
    allMonthData = allExp;

    if (!allMonthDataNumbers) {
        allMonthDataNumbers = [[NSMutableArray alloc]init];

    }
    [[MBProgressHUD HUDForView:self.view] setDetailsLabelText:@"Updating Balances..."];

    for (NSArray *current in allMonthData) {
        Expense *exp = [current objectAtIndex:0];
        NSNumber *monthNumber = exp.month;
        double budget = 0;
        double spent = 0;
        double balance = 0;
        int count = 0;
        double avgDayBal = 0;

        for (Expense *exp in current) {                   // iterate this month
            if (exp.expenseType.boolValue == 0) {                 // all day type expensees
                spent = spent+exp.value.doubleValue;
                count ++;
            }
            else if (exp.expenseType.boolValue == 1) {
                budget = budget+exp.value.doubleValue;
            }
        }
        balance = budget+spent;
        avgDayBal = balance/[dataHandler numberOfDaysInMonthForMonth:monthNumber];

        NSMutableArray *temp = [[NSMutableArray alloc]init];
        [temp addObject:monthNumber];
        [temp addObject:[NSNumber numberWithDouble:budget ]];
        [temp addObject:[NSNumber numberWithDouble:spent ]];
        [temp addObject:[NSNumber numberWithDouble:balance ]];
        [temp addObject:[NSNumber numberWithInt:count ]];
        [temp addObject:[NSNumber numberWithDouble:avgDayBal ]];

        [allMonthDataNumbers addObject:temp];
    }
    NSNumber *day = [dataHandler getDayNumber:[NSDate date] ];
    [[allMonthDataNumbers lastObject] addObject:day];

    NSLog(@"We have %d month", [allMonthDataNumbers count]);

    [[MBProgressHUD HUDForView:self.view] setDetailsLabelText:@"Updating Interface..."];


    [allMonthTable reloadData];
    isLoadingAllMonth = NO;

}

代码:reloadThisMonth

-(void)reloadThisMonth{

    [dataHandler updateData];

    if (!tableData) {
        tableData = [[NSMutableArray alloc]initWithCapacity:31];
    } 

    for (NSMutableArray *temp in tableData) {
        [temp removeAllObjects];
    }
    [tableData removeAllObjects];

    for (int j = 0; j < 31; j++) {          //fill with 31 empty mutuable arrays
        NSMutableArray *tempArray = [[NSMutableArray alloc]init];
        [tableData addObject:tempArray];
    }

    for (Expense *exp in dataHandler.allMonthExpenses) {
        if (exp.expenseType.boolValue == 0) {
            [[tableData objectAtIndex:(exp.day.intValue-1)]addObject:exp];
        }
    }
    int countDayExp = 0;
    for (NSMutableArray *arr in tableData) {
        countDayExp = countDayExp + arr.count;
    }
    if (countDayExp == 0) {
        hasDayExpenses = NO;
    }
    else{
        hasDayExpenses = YES;
    }

    [thisMonthTable reloadData];
    [thisMonthTable setBackgroundColor:[self grayColor]];

}

有谁看到我哪里出错了?或者还有什么问题?两张桌子都很好。如果我在加载第二个视图时不与应用程序交互,一切都很完美。任何想法?

更新

显然两个线程在同时抓取相同的fetchroutine时发生冲突 - 这是应用程序“挂起”时暂停调试状态的屏幕截图。如果我打开另一个需要相同提取例程的选项卡,应用程序会挂起。如果我调试&amp;在挂起状态暂停它会在提取例程中显示一条线。这是我第一次使用线程 - 我真的很感激如何避免这种冲突:/

threads colliding

1 个答案:

答案 0 :(得分:0)

在另一个线程中运行任务时,我遇到了与MBProgressHUD类似的问题。我解决它的方法是在任何地方使用* HUD属性而不是局部变量。看起来你在某些地方使用局部变量而在其他地方使用实例变量。

@property (strong, nonatomic) MBProgressHUD *HUD;

然后使用它:

HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.delegate = self;

在另一个线程中执行你的代码,然后使用GCD调用主线程来隐藏它并删除HUD:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {

//Your Code to execute in the background

      dispatch_async(dispatch_get_main_queue(), ^(void) {
             //Code here to update stuff on main thread
             //Example: hide hud or change hud label
                    HUD.labelText = @"Foo Done, Bar started"; //change label
                    [HUD hide:YES];                          // or hide HUD
                    [self.tableView reloadData];            // Or reload tableView
                });

});

MBProgressHUD代表:

- (void)hudWasHidden:(MBProgressHUD *)hud {
    // Remove HUD from screen when the HUD was hidded
    [HUD removeFromSuperview];
    HUD = nil;
}

方法:

[HUD showWhileExecuting:@selector(reloadAllMonth) onTarget:self withObject:nil animated:YES];

与遇到最麻烦的地方相同。看起来HUD不知道您的视图已被更改并且认为选择器未完成并且继续运行阻塞主线程。所以我摆脱了那个方法并使用了实例Variable * HUD和GCD,每当我需要更新HUD的标签或者隐藏/删除它时检索主线程,如上面的代码所示。