我刚刚成功地在我的iPhone应用程序中实现了skpsmtpmessage。这可以很好地工作,但它在主线程上运行,导致UI锁定,直到操作完成。因此,我尝试将其移动到第二个线程:
[NSThread detachNewThreadSelector:@selector(launchJobWithJob:) toTarget:self withObject:jobDescription];如果我这样做,这个类似乎马上就卡在连接上了,唯一的NSLog输出是:
C: Attempting to connect to server at: mail.example.com:25如果我只是通过[self launchJobWithJob:jobDescription];来启动工作,它工作得很好,但正如我之前所说的,它会严重滞后。
我怎样才能让它在后台线程中工作?有没有人碰到过这个?
编辑:我也尝试过NSOperationQueue --同样的情况再次发生,只有日志输出,没有其他的!
NSOperationQueue*queue = [NSOperationQueue new];
NSInvocationOperation*operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(launchJobWithJob:) object:jobDescription];
[queue addOperation:operation];
[operation release];发布于 2010-12-22 22:26:47
我确信它在runloop上做网络连接的事情,所以让线程的runloop运行,直到操作完成。
[[NSRunLoop currentRunLoop] run];发布于 2010-12-22 22:37:44
必须有一些竞争条件,你可以在你的launchJobWithJob方法中放置一些NSLog来检测哪个代码导致了问题。
https://stackoverflow.com/questions/4482636
复制相似问题