NSThread with _NSAutoreleaseNoPool error

前端 未结 5 603
独厮守ぢ
独厮守ぢ 2021-02-04 09:38

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

5条回答
  •  无人共我
    2021-02-04 10:29

    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 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:
    

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

提交回复
热议问题