为什么在加载完成后多次执行操作以及为什么排队所有事件?

时间:2016-02-01 06:18:02

标签: ios iphone uitableview cocoa-touch touch

当我们点击后退按钮或表格的任何单元格时加载,在加载完成后多次调用该动作。这是我开始加载时停止加载的代码片段

+(void)showLoader_OnView{
      APP_DELEGATE.window.userInteractionEnabled = NO;
     [MBProgressHUD showHUDAddedTo:APP_DELEGATE.window animated:YES];
}

停止加载: -

+(void)hideLoader {
    APP_DELEGATE.window.userInteractionEnabled =YES;
    [MBProgressHUD hideAllHUDsForView:APP_DELEGATE.window animated:YES];
}

请帮帮我。

更新

实际上我从服务器获取数据。每当用户进入下一个窗口,然后在viewWillAppear函数中,我调用一个函数,它将命中api以获取数据。

-(void)performAutoSync
{
    @try
    {
        if(self.shouldPerformAutoSync)//Necessary conditions to check the auto sync
        {
            [AppConstants showLoader_OnView];  //here i call the loader.
            self.shouldPerformAutoSync  = NO;
            if(!self.isSyncing)
            {
                if(!syncBl)
                {
                    syncBl = [[SyncBL alloc] init];
                    syncBl.delegate = self;
                }

                if(!syncDl)
                    syncDl = [[SyncDL alloc] init];

                //            [self saveModifiedDataForCurrentViewController];


                [self delayToAutoSync];


                NSMutableDictionary *dictMainData = [NSMutableDictionary new];
                [dictMainData setObject:[syncDl fetchCompleteDataAndPrepareDictionary:YES] forKey:@"data"];//@"MainData"];
                [syncBl performAutoSync:dictMainData];
            }
        }
    }
    @catch (NSException *exception) {
        BILog(@"%@",exception);
    }

}

1 个答案:

答案 0 :(得分:2)

Don't block the main thread.

Seeing that you invoke [AppConstants showLoader_OnView] from performAutoSync, and that showLoader_OnView in turn executes:

[MBProgressHUD showHUDAddedTo:APP_DELEGATE.window animated:YES]

I can only assume that performAutoSync is executed in the main thread. This, of course, blocks the UI until your operations are completed.


You should redesign so that you won't need all your state variables, globals, global calls, and take advantage of multi-threading.

Also, remove this, as it qualifies as a kludge;

APP_DELEGATE.window.userInteractionEnabled = NO