NSThread与_NSAutoreleaseNoPool错误 [英] NSThread with _NSAutoreleaseNoPool error

查看:123
本文介绍了NSThread与_NSAutoreleaseNoPool错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,将文件保存到互联网,它的工作,但只是缓慢。然后我想让用户界面更流畅,所以我创建一个NSThread来处理慢任务。

I have an method which save files to the internet, it works but just slow. Then I'd like to make the user interface more smooth, so I create an NSThread to handle the slow task.

我看到的错误列表如下:

I am seeing a list of errors like:

_NSAutoreleaseNoPool(): Object 0x18a140 of class NSCFString autoreleased with no pool in place - just leaking

没有NSThread,方法如下:

Without NSThread, I call the method like:

[self save:self.savedImg];

我使用下面的命令来使用NSThread来调用方法:

And I used the following to use NSThread to call the method:

NSThread* thread1 = [[NSThread alloc] initWithTarget:self
                                        selector:@selector(save:)
                                              object:self.savedImg];
[thread1 start];

谢谢。

推荐答案

首先,你要为你的保存代码创建一个新的线程,然后异步地使用NSUrlConnection。 NSUrlConnection在自己的实现也会分离另一个线程,并调用回你的新创建的线程,这大多不是你想要做的事情。我假设你只是想确保你的UI不会阻止,而你保存...

Well first of all, you are both creating a new thread for your saving code and then using NSUrlConnection asynchronously. NSUrlConnection in its own implementation would also spin-off another thread and call you back on your newly created thread, which mostly is not something you are trying to do. I assume you are just trying to make sure that your UI does not block while you are saving...

NSUrlConnection也有同步版本,将阻塞在你的线程和它将更好地使用,如果你想启动自己的线程做事情。签名是

NSUrlConnection also has synchronous version which will block on your thread and it would be better to use that if you want to launch your own thread for doing things. The signature is

+ sendSynchronousRequest:returningResponse:error:

然后当你得到响应,你可以回调到你的UI线程。下面的东西应该工作:

Then when you get the response back, you can call back into your UI thread. Something like below should work:

- (void) beginSaving {
   // This is your UI thread. Call this API from your UI.
   // Below spins of another thread for the selector "save"
   [NSThread detachNewThreadSelector:@selector(save:) toTarget:self withObject:nil];    

}

- (void) save {
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  

   // ... calculate your post request...
   // Initialize your NSUrlResponse and NSError

   NSUrlConnection *conn = [NSUrlConnection sendSyncronousRequest:postRequest:&response error:&error];
   // Above statement blocks until you get the response, but you are in another thread so you 
   // are not blocking UI.   

   // I am assuming you have a delegate with selector saveCommitted to be called back on the
   // UI thread.
   if ( [delegate_ respondsToSelector:@selector(saveCommitted)] ) {
    // Make sure you are calling back your UI on the UI thread as below:
    [delegate_ performSelectorOnMainThread:@selector(saveCommitted) withObject:nil waitUntilDone:NO];
   }

   [pool release];
}

这篇关于NSThread与_NSAutoreleaseNoPool错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆