何时释放/保留传递给辅助线程的对象?
我使用以下代码将对象传递给辅助线程:
(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 似乎不是正确的方法。处理这个问题的最佳方法是什么?谢谢。
-奥斯卡
I am passing an object to a secondary thread using the following code:
(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];
}
My question is, is autorelease the right way to handle the release of this object?. Once it is passed to the secondary thread I retain it and release it when I no longer need it.
However is it possible that the autorelease, releases the object before the secondary thread has a chance to retain it?. Do I have to create an ivar for this?, so that I can release the object in the performSelectorOnMainThread?. I no longer need the object after the login so an ivar doesn't seem like the right way to go. What is the best way to handle this?. Thank you.
-Oscar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
detachNewThreadSelector:toTarget:withObject:
的文档回答了您的问题:所以是的,您可以自动释放该对象或在调用 detachNewThreadSelector 后显式释放它。并且您不必在辅助线程中保留该对象。
The documentation for
detachNewThreadSelector:toTarget:withObject:
answers your question:So yes, you can autorelease the object or release it explicitly after calling detachNewThreadSelector. And you don't have to retain the object in the secondary thread.
来自文档。
所以你不需要这么努力。 Autorelease 在这里很好,您不需要将其保留在线程中,因为线程本身保留参数和目标,并在完成后释放它。
From the docs.
So you dont need to try this hard. Autorelease is fine here, and you should not need to retain it in the thread since the thread itself retains the argument and the target, and will release it when done.