我看到一个错误列表,如:
_NSautoreleasenopool(): Object 0x18a140 of class NSCFString autoreleased with no pool in place - just leaking
没有NSThread,我称之为:
[self save:self.savedimg];
而且我使用以下方法来使用NSThread来调用该方法:
NSThread* thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(save:) object:self.savedimg];[thread1 start];
谢谢.
解决方法 首先,您正在为您的保存代码创建一个新线程,然后以异步方式使用NSUrlConnection. NSUrlConnection在自己的实现中也会分离另一个线程,并在你新创建的线程上调用你,这主要不是你想要做的.我假设你只是想确保你在保存时不会阻止你的UINSUrlConnection也具有同步版本,这将阻止你的线程,如果你想启动自己的线程做事情,最好使用它.签名是
+ sendSynchronousRequest:returningResponse:error:
然后,当您收到响应后,您可以回调到您的UI线程.下面的东西应该有效:
- (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错误全部内容,希望文章能够帮你解决可可 – NSThread与_NSAutoreleaseNoPool错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)