简体   繁体   English

ARC中的'[[something retain] autorelease]相当于什么?

[英]What's the equivalent of '[[something retain] autorelease]' in ARC?

What's the equivalent of [[something retain] autorelease] in ARC? ARC中[[something retain] autorelease]的等价物是[[something retain] autorelease]

I have a problem where a class DBRequest calls my delegate to signify completion. 我有一个问题,类DBRequest调用我的委托来表示完成。 My delegate then sets the DBRequest instance to nil, which dealloc's it. 我的委托然后将DBRequest实例设置为nil,dealloc就是它。 But then when the stack pops out of my delegate and jumps back to the DBRequest, it of course then crashes. 但是当堆栈弹出我的委托并跳回DBRequest时,它当然会崩溃。

If I wasn't in ARC, in my delegate I'd simply do [[theDbRequest retain] autorelease] before releasing my reference to it, so that it'd survive long enough until the next run loop autoreleased it. 如果我不在ARC中,在我的委托中,我只是做[[theDbRequest retain] autorelease]然后释放我对它的引用,这样它就能存活足够长的时间直到下一个运行循环自动释放它。

What should I do in ARC? 我应该怎么做ARC?

How about adding something like 如何添加类似的东西

__strong DBRequest * myself = self;
[delegate reportDone];

I think that'll increment the self object until the end of the function preventing it from dying early. 我认为这会增加自我对象,直到函数结束,防止它早死。

My delegate then sets the DBRequest instance to nil, which dealloc's it. 我的委托然后将DBRequest实例设置为nil,dealloc就是它。 But then when the stack pops out of my delegate and jumps back to the DBRequest, it of course then crashes. 但是当堆栈弹出我的委托并跳回DBRequest时,它当然会崩溃。

Surely this was always a bad strategy, and your [[theDbRequest retain] autorelease] was always just covering up the problem, yes? 当然这总是一个糟糕的策略,你的[[theDbRequest retain] autorelease]总是只是掩盖了这个问题,是吗?

Simply do nothing. 什么都不做。 So your instance variable sticks around; 所以你的实例变量坚持不懈; so what? 所以呢? You know that ARC will release it for you when you are dealloced. 你知道当你被解除分配时,ARC会为你发布它。

The important thing is not to release theDbRequest , but to set theDbRequest's reference to you (the delegate) to nil, so it doesn't try to call you back when you no longer exist. 重要的是不要释放theDbRequest ,而是将dbRequest对 (委托)的引用设置为nil,所以当你不再存在时它不会试图回复你。 Your own dealloc would be a good place to do that. 你自己的dealloc将是一个很好的地方。

Hope I'm understanding the issue correctly. 希望我正确理解这个问题。 If not, post some code! 如果没有,发布一些代码!

As @matt says if you simply do nothing ARC should clean up when your object is deallocated - assigning the DBRequest you create to an instance variable handles that ( provided of course your object outlasts the object you are creating). 正如@matt所说,如果你只是什么都不做,那么当你的对象被释放时,ARC应该清理 - 将你创建的DBRequest分配给一个实例变量处理(当然你提供的对象比你正在创建的对象要长)。

If you need to deallocate the DBRequest before your object dies then you need an ARC-compatible "trick" equivalent to [[theDbRequest retain] autorelease] . 如果你需要对象死亡之前释放DBRequest 那么你需要一个与[[theDbRequest retain] autorelease]相当的ARC兼容“技巧”。 Now unless you are constructing your own auto release pools your previous approach would trigger at the end of the current event. 现在,除非您构建自己的自动释放池,否则之前的方法将在当前事件结束时触发。 Following that logic try: 遵循该逻辑尝试:

  1. Add a method to your class which simply sets theDbRequest to nil , let's call this cleanUpTheDbRequest . 在你的类中添加一个方法,只需将theDbRequest设置为nil ,让我们调用cleanUpTheDbRequest
  2. Change your delegate callback to invoke [self performSelectorOnMainThread:@selector(cleanUpTheDbRequest) withObject:nil waitUntilDone:NO] instead of directly assigning nil to theDbRequest 更改你的委托回调以调用[self performSelectorOnMainThread:@selector(cleanUpTheDbRequest) withObject:nil waitUntilDone:NO]而不是直接将nil分配给theDbRequest

This should delay the assigning of nil till after the end of the current event, just as your autorelease "trick" did. 这应该延迟nil的分配直到当前事件结束之后,就像你的自动释放“技巧”那样。 It also works if your DBRequest lives across multiple events - the previous method kicks in at the end of the event the autorelease is called in, this method at the end of the event the delegate method is called in. 如果您的DBRequest存在于多个事件中,它也会起作用 - 前一个方法在调用autorelease的事件结束时启动,此方法在事件结束时调用委托方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM