问题描述
我有一种情况,我懒加载图像从www。
这是一个项目列表,当一个项目被点击,一个详细视图被推送到导航控制器。
I have a situation where I'm lazy loading images from the www.
It's a list of items, when one item is tapped, a detail view is pushed to a nav controller.
在该详细视图中,该项目有一个图片,首先是默认图片,我想从网址开始加载图片。
In that detail view the item has an image, which first is a default image, and I want to start loading it's image from a URL.
所以我做的是创建一个对象,一旦初始化分离一个新的线程,反过来加载内容,然后通知我的观点,数据加载:
So what I do is create an object which once initialized detaches a new thread which in turn loads the content and afterwards notifies my view that the data is loaded:
// in MyLoader:
- (MyLoader *)initWithUrl:(NSURL *)url requester:(id)requester {
self.url = url;
self.requester = requester; // both are nonatomic, retain properties
[self performSelectorInBackground:@selector(loadIt) withObject:nil];
}
- (void)loadIt {
NSAutoreleasePool *arp = [[NSAutoreleasePool alloc] init];
NSData *data = [NSData dataWithContentsOfURL:url];
[requester performSelectorOnMainThread:@selector(dataReady) withObject:data waitUntilDone:YES;
[arp release];
}
// in MyRequester:
- (void)somewhere {
MyLoader *loader = [[[MyLoader] alloc] initWithUrl:someUrl requester:self] autorelease];
// then I retain loader somewhere, it's more complicated but I have verified that it's properly retained.
}
几个备注:
-
首先我认为一些变量可能有问题。我在
performSelectorOnMainThread
之前放置一个断点,并确认数据
和请求者$ c $
First I thought there might be a problem with some of the variables. I put a breakpoint right before
performSelectorOnMainThread
and confirmed thatdata
andrequester
were both OK.
然后我认为这是由于传递NSData跨线程造成的,所以我改变了 withObject:nil
。它仍然崩溃。
Then I thought it was caused by passing the NSData across the threads, so I changed withObject:nil
. It still crashes.
当我进一步调查,崩溃是非常奇怪。我指定 waitUntilDone:YES
,我在请求者
的 dataReady
。但是 performSelectorOnMainThread
调用返回(它到达之后的断点),而不到达 dataReady
中的断点。 BTW, - (void)dataReady:(NSData *)
的主体现在只包含 int x = 1;
(要在其上放置断点)。此外,我尝试设置 waitUntilDone:NO
,它仍然崩溃。
When I further investigated, the crash was very strange. I specified waitUntilDone:YES
, I've placed a breakpoint in the requester
's dataReady
. But the performSelectorOnMainThread
call returns (it reaches the breakpoint after it) while not reaching the breakpoint inside dataReady
. BTW, - (void)dataReady:(NSData*)
's body for now only contains int x = 1;
(to place a breakpoint on). Also, I've tried setting waitUntilDone:NO
, it still crashes.
没有人执行(没有达到断点)有什么问题吗?
Does anyone have any idea what's wrong?
这很明显,只是为了清楚,如果我只是注释 [requester performSelectorOnMainThread ...
部分,它不会崩溃。
This is obvious, but just to be clear, if I just comment out the [requester performSelectorOnMainThread...
part, it doesn't crash.
此外,这里是一个堆栈跟踪,但它没有任何帮助。
Also, here's a stack trace, but it's not helpful at all.
#0 0x00a71004 in ___TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION___ ()
#1 0x93436e3b in objc_exception_throw ()
#2 0x0028aca6 in __NSThreadPerformPerform ()
#3 0x00a098e1 in CFRunLoopRunSpecific ()
#4 0x00a08c48 in CFRunLoopRunInMode ()
#5 0x0005a78d in GSEventRunModal ()
#6 0x0005a852 in GSEventRun ()
#7 0x0168a003 in UIApplicationMain ()
#8 0x000028d4 in main (argc=1, argv=0xbffff100) at /Users/myName/Document/appName/main.m:14
推荐答案
您拥有:
[requester performSelectorOnMainThread:@selector(dataReady) withObject:data waitUntilDone:YES;
应为:
[requester performSelectorOnMainThread:@selector(dataReady:) withObject:data waitUntilDone:YES;
注意: @selector(dataReady :)(带冒号)
因为你向方法传递一个参数,所以假定数据准备就是这样定义的:
notice: @selector(dataReady:) (with colon)Since you're passing an argument to the method, it's presumed data ready is defined something like:
- (void) dataReady:(NSData *)theData ...
这篇关于Cocoa-Touch:performSelectorOnMainThread:怪异的行为+崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!