Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top