Search code examples
iosobjective-ciphonesingletonblock

How to use singleton object into a objective-C Block


 Using a singleton object in a block will create a strong reference cycle in the code ?

As I have more then 5 singleton object in application.


Solution

  • You have to use weak reference of singleton inside blocks.

    YourSingleton *singletonInstance = ---- //get your singleton instance here
    
    typeof(YourSingleton) __weak weakSingletonInstance = singletonInstance;
    
    // your block
    ^
    {
       // Now use weakSingletonInstance inside the block.           
    }];