skpsmtpmessage in different thread

时光怂恿深爱的人放手 提交于 2020-01-24 22:46:25

问题


I have just successfully implemented skpsmtpmessage into my iPhone app. This works fine, but it runs on the main thread, causing the UI to lock up until the operation is complete. Therefore I have tried moving it to a second thread:

[NSThread detachNewThreadSelector:@selector(launchJobWithJob:) toTarget:self withObject:jobDescription];

If I do this that way, the class seems to get stuck on the connecting right away, with the only NSLog output being:

C: Attempting to connect to server at: mail.example.com:25

If I launch the job just by going [self launchJobWithJob:jobDescription];, it works fine but as I said before, lags heavily.

How can I get this to work in a background thread? Has someone come across this?

Edit: I have tried NSOperationQueue as well - the same happens again, just the log output and nothing else!

NSOperationQueue*queue = [NSOperationQueue new];

NSInvocationOperation*operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(launchJobWithJob:) object:jobDescription];

[queue addOperation:operation];

[operation release];

回答1:


I'm sure that it does its network connection stuff on the runloop, so let the runloop of the thread run until the operation is finished.

[[NSRunLoop currentRunLoop] run];



回答2:


Must have some race condition, you can put some NSLog at you launchJobWithJob method to detect which code cause problem.



来源:https://stackoverflow.com/questions/4482636/skpsmtpmessage-in-different-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!