Singleton object or Use the class for some Build Configuration only - iOS

I have a singleton class. This class has methods which use core data and are to be used only for some build configuration.

+(AClass*) singletonInstance { static dispatch_once_t dispatchCall; static AClass *shared=nil; dispatch_once(&dispatchCall, ^{ shared=[[AClass alloc] init]; }); return shared; } -(id)init{ self=[super init] if(self){ [self initCoreData]; } return self; }

So, if I use the following code will this be a correct way to handle it. I think, this will be handled at compile time, so at run time it will be always nil, so any method called on

[[AClass singletonInstance] someMethod]

will not work , As a message passed to a nil object, which will not crash but at the same time, it will not respond to that message. Also, it would not init the core data.

Approach 1

+(AClass*) singletonInstance { #if DEBUG==6 static dispatch_once_t dispatchCall; static AClass *shared=nil; dispatch_once(&dispatchCall, ^{ shared=[[AClass alloc] init]; }); return shared; #else return nil; #endif }

Approach 2 I have one more helper class which has singleton method. I have this method and is used in many other classes (So, if in future if I change it to DEBUG preprocessor value to say "10", then I will have to make change at one place only). would be ok to use it over here also?

-(BOOL)shouldInitContent{ #if DEBUG==6 return YES; #else return NO; #endif } +(AClass*) singletonInstance { if([[AHelperClass helperInstance] shouldInitContent]){ static dispatch_once_t dispatchCall; static AClass *shared=nil; dispatch_once(&dispatchCall, ^{ shared=[[AClass alloc] init]; }); return shared; }else{ return nil; } }

Which of the above two approaches could be efficient?

-------------Problems Reply------------

I would go with approach #1, because it will help you minimize the memory consumption.

In the second approach you are using two singleton instance which I doesn't recommend.

Category:ios Views:2 Time:2018-07-11

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.152 (s). 11 q(s)