延迟:不保留目标?

时间:2021-09-12 14:21:18

I've got a class which uses NSURLConnection to open a long-running connection to a server. When the connection's closed, either in connectionDidFinishLoading: or connection:didFailWithError:, I want to wait 15 seconds then retry the connection.

我有一个类,它使用NSURLConnection打开到服务器的长时间连接。当连接关闭时,在connectionDidFinishLoading:或连接:didFailWithError:,我想等待15秒,然后重试连接。

At the moment I'm using [self performSelector:@selector(restartConection) withObject:nil afterDelay:15.0];, but this leads to the undesired situation that when the object is released by its creator, the performSelector and NSURLConnections perpetually retain 'self', and it never gets deallocated.

现在我正在使用[self performSelector:@selector(restartConection) withObject:nil afterDelay:15.0];但是这导致了不希望的情况,当对象被它的创建者释放时,performSelector和NSURLConnections会永久保留“self”,它永远不会被释放。

How can I do this without perpetually retaining the object? Any help'd be much appreciated.

我如何能做到这一点而不永久地保留对象?非常感谢您的帮助。

Thanks, -Alec

谢谢,亚历克

2 个解决方案

#1


5  

You cannot avoid retaining the object. It is retained in order to save you from ugly crashes when in the next main loop cycle the runtime is going to call your selector on the released object.

您无法避免保留该对象。为了避免在下一个主循环周期中,运行时将调用释放对象上的选择器,因此保留它。

If you really insist on having your object released immediately without waiting for your delayed selector, I would suggest you to create a separate proxy class. Say your class is called A, create proxy class B which will have a weak reference to your class A (i.e. __weak A* a) and restartConnection selector which will check if the weak reference is valid. If so it would invoke restartConnection on your A object. Then, do, of course, a delayed selector on B's restartConnection

如果您真的坚持立即释放对象而不等待延迟的选择器,我建议您创建一个单独的代理类。假设您的类被称为A,创建代理类B,它将具有对类A的弱引用(即__weak A* A)和restartConnection选择器,该选择器将检查弱引用是否有效。如果这样,它将调用您的对象上的restartConnection。然后,当然,在B的restartConnection上做一个延迟选择器

But first of all, I would really suggest that you reevaluate whether you really cannot live with the retain.

但首先,我建议你重新评估你是否真的不能接受保留。

#2


5  

I think your only option is to send

我认为你唯一的选择就是发送

[NSTimer cancelPreviousPerformRequestsWithTarget: object];

at some point, probably, before releasing the object. If the timer hasn't been scheduled, this is a no-op, but is not free performance-wise.

在某种程度上,可能是在释放对象之前。如果计时器没有被调度,这是一个禁止操作,但不是免费的性能。

#1


5  

You cannot avoid retaining the object. It is retained in order to save you from ugly crashes when in the next main loop cycle the runtime is going to call your selector on the released object.

您无法避免保留该对象。为了避免在下一个主循环周期中,运行时将调用释放对象上的选择器,因此保留它。

If you really insist on having your object released immediately without waiting for your delayed selector, I would suggest you to create a separate proxy class. Say your class is called A, create proxy class B which will have a weak reference to your class A (i.e. __weak A* a) and restartConnection selector which will check if the weak reference is valid. If so it would invoke restartConnection on your A object. Then, do, of course, a delayed selector on B's restartConnection

如果您真的坚持立即释放对象而不等待延迟的选择器,我建议您创建一个单独的代理类。假设您的类被称为A,创建代理类B,它将具有对类A的弱引用(即__weak A* A)和restartConnection选择器,该选择器将检查弱引用是否有效。如果这样,它将调用您的对象上的restartConnection。然后,当然,在B的restartConnection上做一个延迟选择器

But first of all, I would really suggest that you reevaluate whether you really cannot live with the retain.

但首先,我建议你重新评估你是否真的不能接受保留。

#2


5  

I think your only option is to send

我认为你唯一的选择就是发送

[NSTimer cancelPreviousPerformRequestsWithTarget: object];

at some point, probably, before releasing the object. If the timer hasn't been scheduled, this is a no-op, but is not free performance-wise.

在某种程度上,可能是在释放对象之前。如果计时器没有被调度,这是一个禁止操作,但不是免费的性能。