I have an object that I have created that subscribes to some NSNotificationCenter notifications, but it is being released, since I don’t have a pointer to the instantiation. The instantiation has a delegate that will call a method back to another class I have, so I felt it unnecessary to also have a pointer to it, since the pointer doesn’t serve any purpose.
Basically, I have an instantiation of a class, DelegateListener (name is just for example purposes), which subscribes to some of the default NSNotificationCeneter‘s notifications. This instantiation isn’t assigned to any pointer after the instantiation ends. The instantiation, however, has a property, delegate. I assign a value to that delegate during the instantiation. That delegate implements methods that I want the DelegateListener to call when the notifications that it subscribed to fire.
The problem with this is that the instantiation of DelegateListener that I create gets released, unless if I assign it to a retained pointer. Is there a way to avoid this automatic release of my DelegateListener instantiation within ARC?
You should store the
DelegateListenerinstance in a static variable if you don’t have multiple of them.The pointer does (or at least should) serve a purpose: when you no longer need to listen to the notifications, you should unsubscribe the
DelegateListenerfrom receiving them. At the very least, this should happen when your application goes to the background (unless the point of it is to perform background processing), and when your application terminates.