Difference between singleton and Connection Pooling - singleton

What is difference between singleton and connection pooling? or are they similar?

Singleton is a design pattern that restricts the instantiation of a class to one object.
Connection Pooling is an implementation of Object pool pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand. In this case Objects are Connections.

Related

Are DeviceClient Methods Thread-Safe?

Are the instance methods on Microsoft.Azure.Devices.Client.DeviceClient thread-safe? For example, is it safe to have a single instance of DeviceClient, and execute its methods concurrently from multiple threads?
Our current code base makes multiple concurrent calls to UploadToBlobAsync(), UpdateReportedPropertiesAsync(), and SendEventAsync(), all on the same instance. The documentation does not mention thread-safety at all, but I would like to confirm if the methods are thread-safe or not before reworking our current code base to manually prevent concurrent access to the instance methods.
Yes, DeviceClient is thread-safe, according to https://github.com/Azure/azure-iot-sdk-csharp/issues/1613. So you can safely call any instance methods in parallel using just one instance of DeviceClient.

What is the point of retaining a singleton you've accessed?

Is there any point to using retain on a singleton? I believe the whole point of using the singleton pattern is to keep one global object to access from various classes. What would be a case to use retain on such an object?
Generally, the implementation of retain in a singleton class returns self (not singleton instance) like below:
-(id)retain
{
return self;
}
I recently went through some open source code, and the author repeatedly retained the singleton
object = [[SingletonClass shareObject] retain]
and released it in dealloc.
So when I tried to build this project, it worked at first, then crashed when it attempted to access variables on singleton object later.
What would happen exactly, if I retain a singleton object and attempt to access it?
Just because it's a singleton doesn't mean its lifetime is necessarily the lifetime of the process. For example, you might have some singleton that obtains a resource. But until you need that resource, there's no point in creating it. Likewise, you might know that all instances are done with that resource, so why keep it around? An easy way to keep track of whether a singleton needs to continue to exist is its retain count.
For example, you might have a singleton that represents the video camera built-in to a user's device. There might be different parts of your application that need to access the camera. But it might also be possible to use your app without the camera, or the workflow may dictate that the camera is turned off at some point (for privacy, or whatever). So you don't allocate the singleton until the user initiates an action that starts camera use. They may do several actions with the camera, such as take a picture, scan a QR code, record some video, etc. Then they may end all of those actions and need the memory taken by the camera freed up (especially on a mobile device). So they end those actions. If each camera-related actions retained the camera singleton, then each released it when they were done, you could free up the memory and other resources used by the camera, even though it's still a singleton.
Consistency. Make a point of always retaining objects that you keep references to, even if they're singletons, and you won't make mistakes down the road when you forget to retain objects that reference counts do matter for.
Never hold on to a singleton. There is no guarantee that the underlying library/class/manager doesn't replace that singleton with a new object in the application's lifetime. The point of a singleton access static method is for the current valid singleton to be available at any time to any caller.
Singletons are written by anyone and there is no guarantee that the singleton is not switched. The only guarantee is that a specific class will only have one existing instance at any time.
If you retain a singleton at some point the object you retained could be invalid or even worse, be completely different from the one being held to by the class singleton() method.

Atomicity of properties of delegate such as NSURLConnectionDelegate

Should properties be atomic in NSURLConnectionDataDelegate (NSURLConnectionDelegate) when there are more concurrent connections and properties are accessed by methods being called when connection reaches particular state?
There is no access from other threads to those properties invoked by me.
NSURLConnection's concurrency is run-loop-based, so as long as all the connections for which your object is a delegate are started from the same run loop, you shouldn't need to worry about concurrency in your delegate. If you start multiple connections scheduled on different run loops, then yes, you would need to protect the private state of your delegate objects. Atomic properties are rarely sufficient for this.
In short, you either don't need to worry about this, or atomic properties are unlikely to help. One easy option would be to have all your delegate methods dispatch_async their work to a private serial GCD queue.

Destroy and Rebuild Singleton iOS

i am currently developing an application for iOS which needs to communicate with a hardware device using a Socket Connection.
Therefore i am using a Singleton Object with NSStream. To this point all works as expected.
The problem is if the connection is terminated, or interrupted it is not possible to reopen it (this is stated in the Documentation).
So my idea is to destroy the Singleton and recreate it. This should not interfere with the Singleton Pattern, because it states that there only exists one copy of such a class.
Has anyone an idea how to solve this problem?
Any other solution not involving the recreation of the singleton would be highly appreciated.
Why not put some logic in your singleton class to test if the connection to the device is active. If it has died, close the connection, and open a new one. This is effectively the same thing you are trying to do by destroying a recreating the singleton, but doesn't abuse the singleton pattern quite as much. It should also be simpler, because only the singleton knows about the connection, and thus keeps coupling low.
Singleton are not made to be destroyed, probably you should include a method to re-open the singleton class and close/reset old connections.
The heart of Singleton that lets you create a variable once, and only, this means it will live along your app till it is terminated. We init it as Lazy loading, means when it is used, then initial it. It will allocate in RAM as static, and thus we can call it to reuse anytime, this is really save time.
So MUST NOT destroy Singleton, just do some logic inside it.
Hope this help.

Creating another instance of a singleton class in Objective C

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.