I have a singleton class which is being used throughout the app. I am working on another class that needs to send data to this singleton class, but can send data in bunch which will freeze the main thread.
Should I create another instance of this singleton class or should I should I create a data import utility as a separate class?
Singletons, as the name implies, are meant to have only a single instance floating around. Data freezing the main thread should be dispatched, another instance of a class won't help that.
Create another instance all you want, but don't call it singleton anymore.
Actually you should send this data in another thread and maybe use an NSLock while the data is being sent so you don't have any access errors.
Use:
[self performSelectorOnBackGround:#selector(sendDataToSingleton:) withObject:#"data to send"];
Don't create another instance of the singleton class or the rest of your application won't have access to it since it's a singleton.
Hope it helps.
By definition you should only have 1 instance of a singleton. If it's a properly constructed singleton it should not be possible to have more than 1!
If you are running into issues where your main thread is unresponsive, break the data you need to load into smaller chucks. This way, in between loading different chunks of data, the main thread can process any events it needs to and other objects can access the data in the singleton.
You could also implement a lazy data loading mechanism, where when an object wants information from the singleton, the singleton checks if your new class is waiting to give it new information and then loads it.
Related
I know that class methods in Objective-C get loaded once in memory and stay until the app terminates. I am wondering what this means in terms of performance, in comparison to instance methods.
Question 1.
If I call lots of class methods in my code (say millions of class methods), would this effectively load all of them in memory so that they would never go away?
This feels like a problem compared to if I instead implemented them as instance methods, and only instantiate the class locally whenever I need to access them, which then will be garbage collected later when it goes out of scope.
Question 2
What happens to the local variables inside class methods? Do they get garbage collected after each execution? For example I could create millions of local NSObjects inside a class method. Will they get garbage collected once the method returns?
I'm working with game center and wanted to have a singleton class for accessing the GK functionality which I've setup, but I then introduced a couple of methods which needed a delegate. Obviously delegates can't really work properly with a singleton, but I want/need the data loaded in this class to be loaded once and be there all the time.
Is there a nice way that I'm missing of keeping the data there all the time, but having the class instantiated as and when it's needed?
Yoy say "singleton class", and by that I assume you mean that this class only has class methods. That's fine, you can still use it, since class objects are still objects. That said, you will probably need to maintain state. Each delegate call will include some parameter that allows the object to identify the sender.
What I would probably do myself is create a NSMutableDictionary in an "initialize" method, then have objects register themselves before sending delegate methods, and when they register create another mutableDictionary, and save that in the first one with the sending object as the key (or some other unique identifier).
Every delegate call has to include the sender, and with that you can retrieve the dictionary associated with that object.
I have a custom ViewController class and many instances of it, and I want them all to be able to message the same Model (another custom class, only one instance). Passing pointers to the Model along to new instances of the ViewController seems impractical, especially since the model is lazily instantiated. What is the cleanest, most idiomatic, ARC way to do this?
Usually a singleton in ObjC will have a class method that serves as an accessor for the single instance. The convention is for this to be called either defaultX or sharedX. If your model class is indeed a singleton, you should already have such a method. Since class names are globally available, all you have to do to access the instance anywhere in your program is [MyModelClass sharedModel].
I would like to take a singleton class that gets used by multiple view controllers, copy it, and save it to an array that will be displayed in another view controller with a table view that will show multiple instances of that class. This array will eventually be archived to be retrieve with the same data.
Before I attempt this, is there a way I can duplicate this singleton instance, save it to the array, re-initialize it for the next use, and so on (without getting the same exact previously uninitialized object)?
It's not a singleton that you want.
A singleton must always return the same instance.
What you want is a normal class that maybe have a convenience class method to feed you some pre-populated object.
If it's possible to create more than one instance of an object it's not a singleton anymore.
But your singleton class could hold a variable amount of instance of other class that you wish to display.
So in that way what you are asking could be possible, but without the copy part on the singleton.
I'm not sure where you are going with this and if it's the best way to
go but here is an idea :
you can have a singleton class that would hold an array of an other class. So you could call your singleton like this
TheSingletonClassName *mySingleton = [TheSingletonClassName sharedTheSingletonClassName];
OtherClass *myOtherClass = [mySingleton newOtherClassInstancePlease];
In your newOtherClassInstancePlease method you implement the necessary thing to store that new object into an array, that you can distribute like this
NSArray *otherClassArray = [mySingleton allOtherClasses];
Or NSMutableArray if you prefer.
With that you would be able to share, create new and even delete object. if you implement the necessary method on your singleton.
But again don't copy a singleton, if the singleton is well implemented sending it a copy call should throw an exception, or return the single singleton instance that exists.
I've just been looking at the header files for NSObject, and I'm creating a singleton class. The problem I have is that it needs to load up basically from the start of the application, as the singleton maintains state for the application by observing NSNotifications and reporting the current state when asked.
My solution to this was to use the +(void)load method on NSObject. When the class loads into memory, I was going to override the load method, and allocate the singleton at that stage.
The documentation from Apple does not make it clear if it is safe to call [[*class* alloc] init] during the load method. I would generally assume it was, unless it was documented, but considering the importance of this item, and the fact that I'm loading this every time the app launches, I'd like to know anyone has any knowledge of whether this is safe or not, just for reassurance.
Many thanks for any assistance.
It depends on what exactly your class (and its superclasses) does when you call init. During load, other classes are not guaranteed to be available. If your class's init doesn't need any other classes, you might be able to get away with it.
It would be much safer to do this in + (void)initialize instead, and if necessary to call a setup function during application launch (e.g. from application:didFinishLaunchingWithOptions: on iOS).