iphone - 何时释放/保留传递给辅助线程的对象?

标签 iphone variables nsthread arguments retain

我使用以下代码将对象传递给辅助线程:

 (void)login:(id)sender
{
  platformMsgs_LoginRequest *loginRequest = [[[platformMsgs_LoginRequest alloc] init] autorelease];
//more code...
 [NSThread detachNewThreadSelector:@selector(sendLoginRequest:) toTarget:self withObject:loginRequest];
//more code...
}

- (void)sendLoginRequest:(platformMsgs_LoginRequest *)loginRequest
 {
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 [loginRequest retain];
 NetSuiteBinding *binding = [NetSuiteServiceSvc NetSuiteBinding];
 NetSuiteBindingResponse *response = [binding loginUsingParameters:loginRequest      applicationInfo:nil partnerInfo:nil];
 [self performSelectorOnMainThread:@selector(loginOperationCompleted:)   withObject:response waitUntilDone:NO];
 [loginRequest release];
 [pool drain];
 }

我的问题是,autorelease 是处理该对象释放的正确方法吗?一旦它被传递到辅助线程,我就保留它并在不再需要它时释放它。

但是,自动释放是否有可能在辅助线程有机会保留该对象之前释放该对象?我是否必须为此创建一个ivar?以便我可以在performSelectorOnMainThread 中释放该对象?登录后我不再需要该对象,因此 ivar 似乎不是正确的方法。处理这个问题的最佳方法是什么?谢谢。

-奥斯卡

最佳答案

detachNewThreadSelector:toTarget:withObject: 的文档回答了您的问题:

The objects aTarget and anArgument are retained during the execution of the detached thread, then released.

所以是的,您可以自动释放该对象或在调用 detachNewThreadSelector 后显式释放它。而且您不必在辅助线程中保留该对象。

关于iphone - 何时释放/保留传递给辅助线程的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2233233/

相关文章:

iphone - 如何制作 URL/电话可点击的 UILabel?

ios - 在 IOS 设备上模拟 GPS 位置

javascript - 在javascript中作为按钮值的变量

ios - 从 Swift 中的特定线程运行代码

objective-c - 命令行 Objective-C 程序如何创建其主 NSThread 线程?

iphone - UICollectionView 中的大单元格在单元格仍显示时被删除

iphone - 在启用分页和多个图像的情况下缩放 UIScrollView

r - R 中长表达式的简写名称

Javascript:从字符串初始化变量?

objective-c - NSOperationQueue 中的线程命名