UIView和UITableView,reloadData奇怪的延迟 [英] UIView and UITableView, reloadData weird latency

查看:127
本文介绍了UIView和UITableView,reloadData奇怪的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对视图控制器的重绘有些奇怪。视图控制器包含一个UITableView和一个微调器。

I have something weird with the repaint of my view controller. The view controller contains an UITableView and a spinner.

我有一个 updateFeed 函数(由IBOutlet触发),它带来了微调器在我的视图控制器前面,并将 doUpdate 函数放入NSOperationQueue。

I have an updateFeed function (fired by an IBOutlet) who brings the spinner in front of my view controller and puts a doUpdate function into an NSOperationQueue.

- (void)updateFeed {
    [self showMuar];
    ResourceLoader *loader = [ResourceLoader sharedResourceLoader];
    [loader updateFeed:[self feed] forDelegate:self];
    isReloading = YES;
}
- (id<ResourceToken>)updateFeed:(Feed *)feed forDelegate:(id)delegate {
     return [self queueOperation:[[Operation alloc] initWithObject:feed withSelector:@selector(doUpdate) delegate:delegate]];
}

doUpdate 函数调用解析器(startParserWithAdd :)函数从Internet检索内容并为UITableView格式化此内容,然后执行 [tableView reloadData]

The doUpdate function calls a parser (startParserWithAdd:) function who retrieves content from internet and format this content for the UITableView and then perform a [tableView reloadData].

- (NSError *)doUpdate {
    [self startParserWithAddThreaded:NO];
    return nil;
}
- (void)startParserWithAddThreaded:(BOOL)add {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [self startParserWithAdd:add];
    [pool release];
}

...解析器函数中的某个地方(startParserWithAdd :)

... somewhere in the parser function (startParserWithAdd:)

[tableView setDataSource:self];
[tableView reloadData];

完成 doUpdate 后没有错误,感谢我的操作课 loadingResourceDidFinish 在我的视图控制器中执行。 loadingResourceDidFinish 在后台发送微调器

Once the doUpdate done without errors and thanks to my Operation class the loadingResourceDidFinish is performed in my view controller. loadingResourceDidFinish send the spinner in background.

- (void)loadingResourceDidFinish:(Feed*)f {
    isReloading = NO;
    [self loadingResourceDidFinishPostAction];
}
- (void)loadingResourceDidFinishPostAction {
        [self hideMuar];
}
- (void)hideMuar {
    [loadingIndicator stopAnimating];
    [self.view sendSubviewToBack:muar];
}

一切正常但是旋转器在 loadingResourceDidFinish 已经完成。

Everything works fine but the spinner is sent to background about 4s after the loadingResourceDidFinish is finished.

准确地说,muar是一个不透明的UIView,它包含微调器。我只是想在我原来的解释中抽象出复杂性!

To be exact, muar is an UIView with opacity who contains the spinner. I just wanted to abstract complexity in my original explaination !

我试着用NSLog这个,听起来好像没有重新绘制,直到reloadData完成。我认为这是一个重新出现的问题,因为如果我在 loadingResourceDidFinish 结束后转向我的iPhone,一切都很好,微调器消失了。

I tried to NSLog this and it sounds like nothing is repainted until the reloadData is complete. I think it's a repaint problem because if i turn my iPhone in landscape just after the end of loadingResourceDidFinish, everything is well, the spinner disapears.

我试图删除解析器函数中的NSAutoreleasePool,但不会更改。

I tried to remove the NSAutoreleasePool in the parser function but doesn't change.

我肯定误解了一些东西,但我不知道是什么它是......

I surely misunderstood something but i don't know what it is ...

我改变了代码使它不那么混乱。 UITableView的reloadData现在放在这里:

I changed the code to make it less confusing. The reloadData of the UITableView is now placed here :

- (void)loadingResourceDidFinishPostAction {
    [tableView reloadData];
    [self hideMuar];
    NSLog(@"loadingResourceDidFinishPostAction end");
}

对于调试:

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cellForRowAtIndexPath %i", indexPath.row);
        ...

那么为什么第一个细胞重新粉刷后仅仅5s loadResourceDidFinishPostAction结束?

2010-09-02 10:15:35.279 myapp[9632:5b03] loadingResourceDidFinishPostAction end
2010-09-02 10:15:40.208 myapp[9632:5b03] cellForRowAtIndexPath 0
2010-09-02 10:15:40.697 myapp[9632:5b03] cellForRowAtIndexPath 1
2010-09-02 10:15:40.878 myapp[9632:5b03] cellForRowAtIndexPath 2

这是同样的事情使用我的iPhone 3G和iPhone 4模拟器。

It is the same thing with my iPhone 3G and the iPhone 4 simulator.

推荐答案

我不确定是什么原因导致您的延迟,但它闻起来像它可能是一个线程问题。许多UIKit类都不是线程安全的。作为一般规则,您应该尝试确保所有UI交互都发生在主线程上。在您最初发布的代码中,看起来您正在从后台线程调用reloadData,这似乎有风险。我不清楚从现在开始调用哪个线程你已经改变了你的代码。

I'm not sure what's causing your delay, but it smells like it could be a threading issue. Many UIKit classes aren't thread safe. As a general rule, you should try to make sure that all of your UI interaction happens on the main thread. In the code you originally posted, it looked like you were calling reloadData from your background thread, which seems risky. It's not clear to me which thread it's being called from now that you've changed your code.

通过你的doUpdate代码路径并确保任何回调给你的可能导致UI更新的委托正在通过 performSelectorOnMainThread:完成。

Go through your doUpdate code path and make sure that any calls back to your delegate that could lead to a UI update are being done via performSelectorOnMainThread:.

这篇关于UIView和UITableView,reloadData奇怪的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆