Creating another instance of a singleton class in Objective C

Go To StackoverFlow.com

1

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?

2012-04-05 20:32
by iosdevnyc
Not to be too picky but, if you create another instance, it stops being a singleton and loses whatever value there was in guaranteeing only one object. Maybe that's OK but you've changed your pattern from singleton to object pool (or something) - Phillip Mills 2012-04-05 20:36
While flawed, I'd love to see someone on SO invent the doubleton - CodaFi 2012-04-05 20:43
doubleton is funn - iosdevnyc 2012-04-05 20:48


5

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.

2012-04-05 20:36
by CodaFi


0

Create another instance all you want, but don't call it singleton anymore.

2012-04-05 20:39
by Seva Alekseyev
Creating another instance would break the pattern and all the application logic that was based in singleton. It's a bad approach - Raphael Ayres 2012-04-05 20:42
Depends on how relevant to the rest of the program its singleton nature is. I saw a few cases when a previously-singleton class was rebuilt into allowing multiple instances - Seva Alekseyev 2012-04-05 20:44


0

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.

2012-04-05 20:40
by Raphael Ayres


0

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.

2012-04-05 20:45
by Colin D
Ads