skpsmtpmessage in different thread

2019-08-12 15:13发布

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];

2条回答
等我变得足够好
2楼-- · 2019-08-12 15:56

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];
查看更多
男人必须洒脱
3楼-- · 2019-08-12 16:18

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

查看更多
登录 后发表回答