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.
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.
How can I do this without perpetually retaining the object? Any help’d be much appreciated.
Thanks, -Alec
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 classBwhich will have a weak reference to your classA(i.e.__weak A* a) andrestartConnectionselector which will check if the weak reference is valid. If so it would invokerestartConnectionon yourAobject. Then, do, of course, a delayed selector onB‘srestartConnectionBut first of all, I would really suggest that you reevaluate whether you really cannot live with the retain.