0

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?

Jason George
  • 6,992
  • 8
  • 44
  • 60

3 Answers3

0

I was never able to fully resolve this issue. I finally decided to refactor the app using newer block-based HTTP request methods.

Jason George
  • 6,992
  • 8
  • 44
  • 60
0

Why not use Apple's recommended code for singletons?

sosborn
  • 14,676
  • 2
  • 42
  • 46
  • Hmm.. I attempted to do this as well, but my implementation was slightly different. This solution was causing my computer to freeze when executing in the simulator. Perhaps neglecting to write the memory management routines is where I went wrong. – Jason George Mar 08 '12 at 14:31
  • Even people from apple dont like the "official" singleton implementation: http://stackoverflow.com/a/5720476/852828 – jrturton Mar 08 '12 at 15:40
  • They might not like it but that doesn't mean it won't work. Try making your implemation exactly as they do and see if it works. – sosborn Mar 08 '12 at 23:16
0

Sure sounds like the singleton class and its properties are not yet fully ready to go when you call them.

You mention doing a lot of network stuff in the init method. Any chance the object is not in a fully ready state when you call it?

If you have a lengthy process to complete before you can safely use an object, you might look at using a notification or delegate pattern to run the next step when things are ready to go.

For the case where the process bombs with no warnings, try using NSZombies to assist in the debug.

giff
  • 1,720
  • 1
  • 14
  • 21