PerformSelector: afterDelay: not saving a target?

I have a class that uses NSURLConnection to open a long-term connection to the server. When the connection is closed, either in connectionDidFinishLoading: or connection: didFailWithError :, I want to wait 15 seconds and then retry the connection.

I am currently using [self performSelector:@selector(restartConection) withObject:nil afterDelay:15.0]; , but this leads to an undesirable situation when, when the object is released by its creator, the performSelector and NSURLConnections functions constantly save the "I" and are never released.

How can I do this without constantly saving the object? Any help would be greatly appreciated.

Thanks, -Alec

+4
source share
2 answers

You cannot avoid saving the object. It is saved in order to save you from ugly crashes when, in the next loop of the main loop, the runtime will call your selector on the released object.

If you really insist on immediate release of your object, without waiting for your pending selector, I would suggest you create a separate proxy class. Say your class is called A , create a proxy class B that will have a weak link to your selector A (i.e. __weak A* a ) and restartConnection , which will check for the weak link. If so, it will reference restartConnection on your object A Then make, of course, the delayed selector on B restartConnection

But first of all, I would really suggest you reconsider whether you really cannot live with conservation.

+4
source

I think your only option is to send

 [NSTimer cancelPreviousPerformRequestsWithTarget: object]; 

at some point, possibly before the release of the facility. If the timer was not scheduled, this does not work, but is not free in performance.

+4
source

Source: https://habr.com/ru/post/1391159/


All Articles