I'm in the process of porting some iOS3 code over to iOS5. Originally I was using a singleton for a global data store. It was providing a single instance of any data I needed to access across view controllers and was primarily populated with data I pull from a server.
In the iOS3 version I used Matt Gallagher's singleton implementation for my data store. With iOS5, however, I want to use ARC so I moved to Luke Redpath's GCD implementation.
Now I'm having trouble with some of the code that pulls server data. In the singleton init function I attempt to populate the singleton by polling my server. Server communication is handled by a separate class. The communication class uses credential data stored in the singleton and this seems to be where I'm running into trouble. Stepping through the code, my server com routine exits when I hit the line of code that accesses the singleton. No error, no warning, the routine just exits when it hits the singleton access line.
The singleton is working correctly in other places so I assume this has something to do with the circular reference to the singleton (singleton instantiates the server class that attempts to access the singleton).
At this point I'm not sure how to proceed. I would rather not restructure my code for the port. Any suggestions on a) what my issue might be and b) other alternatives to create a single, globally accessible, instance of my server data?
EDITED
Using Apple's singleton design pattern, as suggested by sosborn, causes issues with the following block of code in the init routine of my singleton.
// enable the location controller
locationController = [[LocationController alloc] init];
locationController.delegate = self;
[locationController.locationManager startUpdatingLocation];
On the last line I get an EXC_BAD_ACCESS
with
warning: Unable to restore previously selected frame.
I assume this is a memory management issue. Based on Apple's documentation, I did not override any of the memory management routines.
If you want a singleton instance (created and controlled by the class factory method) but also have the ability to create other instances as needed through allocation and initialization, do not override allocWithZone: and the other methods following it as shown in Listing 2-15.
Did I do this incorrectly?