【问题标题】:Activity indicator as one table view loads another一个表视图加载另一个表视图时的活动指示器
【发布时间】:2010-08-19 11:58:44
【问题描述】:

我已经尝试了大约一天来让表格单元格在加载新视图时显示活动指示器。在 didSelectRowAtIndexPath 之后,我想在以下运行时显示指标

[self.navigationController pushViewController:subKeywordController animated:YES];

这个控制器然后运行一个相当密集的 SQL 查询

我已经在网上搜索并阅读了数十篇帖子,但似乎都没有帮助解决我的具体问题。我知道我需要在另一个线程中运行指标,因为推送和后续加载优先,但我不确定该怎么做。我之前考虑过在控制器中运行 SQL 查询,但这变得非常混乱。

奇怪的是,我一直在使用MBProgressHUD 在同一个表格视图中显示繁忙的光标,没有任何问题。只有当我应用搜索然后选择导致此错误的结果之一时:

bool _WebTryThreadLock(bool), 0x1d79b0:试图从主线程或web线程以外的线程获取web lock。这可能是从辅助线程调用 UIKit 的结果。现在崩溃了...

应用程序在 iPhone 上继续运行,但模拟器崩溃。

任何帮助将不胜感激。

【问题讨论】:

    标签: iphone activity-indicator


    【解决方案1】:

    问题是您在控制器中的任务阻碍了 UI 代码(但您可能已经知道这一点!)。解决此问题的一种廉价且简单的方法是使用计时器在启动慢速任务(在本例中为 SQL 查询)时稍作延迟:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // instead of running the SQL here, run it in a little bit
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(doSQL:) userInfo:nil repeats:NO];
    
        // Show the please wait stuff
        [activityIndicator setHidden:NO];
    }
    
    - (void)doSQL:(NSTimer *)timer {
        // Do your sql here
    }
    

    解决这个问题的另一种方法是将你的 SQL 移动到一个单独的线程中:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // instead of running the SQL here, run it in a little bit
        [self performSelectorInBackground:@selector(doSQL) withObject:nil];
    
        // Show the please wait stuff
        [activityIndicator setHidden:NO];
    }
    
    - (void)doSQL {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        // Do your sql here
    
        // Tell the main thread
        [self performSelectorOnMainThread:@selector(doneSQL) userInfo:nil waitUntilDone:YES];
    
        // Cleanup
        [pool release];
    }
    
    - (void)doneSQL {
        // Update your UI here
    }
    

    希望有帮助!

    【讨论】:

    • 谢谢。我来这里是为了删除它,因为我没有调用“resignFirstResponder”是一个愚蠢的错误,但是感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 2011-09-14
    • 1970-01-01
    相关资源
    最近更新 更多