我刚刚在我的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];
答案 0 :(得分:1)
我确信它在runloop上运行它的网络连接,所以让线程的runloop运行直到操作完成。
[[NSRunLoop currentRunLoop] run];
答案 1 :(得分:0)
必须有一些竞争条件,你可以在你的launchJobWithJob方法中放一些NSLog来检测哪个代码会导致问题。