On XMPPFramework delegate method not being called

I have wasted some hours trying to understand, why a delegate method is not called and finally found solution. This is pretty common problem, as I’ve seen in the internet during a search, when I tried to resolve it. That is why I decided to write a little post on a solution. It is also pretty common problem, because a thing, that I discovered concerns not only XMPPFramework classes, but also many other similar classes.

So, the reason of this problem is that most probably your class, that is a delegate for xmppStream and that contains xmppStream is actually released from memory or somehow becomes inactive. This problem occurs because in ARC everything that is created inside a method (and is not an instance variable) is released, when method finishes. And delegate methods cannot be called, that are invoked by some other background process.

I have a class, called EjabberdRegistrator, it contains XMPPStream as a property and it is a delegate of this XMPPStream.

This is how I was initializing this class:

Because ejabberdRegistrator instance is not used anywhere inside a method where it was created, it was released by ARC after method was finished, despite the fact, that xmppStream was still working. That is why xmppStream delegate method was not called.

After some thoughts I finally discovered this and made my ejabberdRegistrator instance a property and created it like this:

This worked! So, it is very important to make a class, that is a delegate of some other class, that is contained inside it make persistent from releasing from memory.

Note that this also can be achieved by creating shared instance of that class using dispatch_once.