0

Greetings,

I'm trying to write my first iPhone app. I have the need to be able to access data in all views. The data is stored when the user logs in and needs to be available to all views thereafter.

I'd like to create a static class, however I when I try to access the static class, my application crashes with no output on the console.

Is the only way to write data to file? Or is there another cleaner solution that I haven't thought of?

Many thanks in advance,

skaffman
  • 398,947
  • 96
  • 818
  • 769
Eamorr
  • 9,872
  • 34
  • 125
  • 209

2 Answers2

7

Use a singleton class, I use them all the time for global data manager classes that need to be accessible from anywhere inside the application. You can create a simple one like this:

@interface NewsArchiveManager : NetworkDataManager
{
}

+ (NewsArchiveManager *) sharedInstance;
@end

@implementation NewsArchiveManager

- (id) init
{
    self = [super init];
    if ( self ) 
    {
         // custom initialization goes here
    }

    return self;
}


+ (NewsArchiveManager *) sharedInstance
{
    static NewsArchiveManager *g_instance = nil;

    if ( g_instance == nil )
    {
        g_instance = [[self alloc] init];
    }

    return g_instance;
}


- (void) dealloc
{
    [super dealloc];
}

@end
Bogatyr
  • 19,255
  • 7
  • 59
  • 72
  • The code you provide seems to be a standard practice and I see it everywhere. But I was always wondering. Won't this **sharedInstance** method cause memory leak? There is only alloc and init, but no dealloc/release in place to release that part of memory... – Di Wu Feb 18 '11 at 15:46
  • When i'm making singleton classes, I like to have the 3 methods (CreateInstance, GetInstance, DeleteInstance). Only having GetInstance is confusing since the user doesn't know whether he has to release the object or not. By using CreateInstance() the user will figure out that it has to match it with a call to DeleteInstance(). – Andrei Stanescu Feb 18 '11 at 15:49
  • 1
    The general notion of a Singleton is that it lives forever, so no, in that regard, it's not a "leak", because a "leak" is when an unaccessible object is not deallocated, and a singleton, is always accessible. But I catch your drift, and yes, the singleton takes up memory. You could always implement a special "deallocSharedInstance" or something to free it and set the g_instance to nil if you know you won't use it for a while and want the memory back. – Bogatyr Feb 18 '11 at 15:51
  • 1
    @Andrei -- Yeah, but I find that's overkill most of the time. The objects themselves as I use them tend to never, ever go away during the lifetime of the application. Your interface has a confusion, too: "should I create it before I get it??". The sheer simplicity of the implicit create is a feature, not a bug IMO. Use it and forget it. Simplicity is a feature, – Bogatyr Feb 18 '11 at 15:54
  • I've never used a singleton before, but won't this code alloc and init a new instance each time the static method sharedInstance is called? Doesn't that defeat the singleton behavior? – jakev Feb 18 '11 at 16:04
  • No, because after the first call, g_instance is no longer nil, so with no dealloc api, the g_instance lives forever and is created exactly one time. – Bogatyr Feb 18 '11 at 16:08
  • Hey, thanks for getting back to me. Sorry I took so long to reply - I was out for an extended coffee with an old friend ;) But yes, you're right, singleton classes are the way to go. Thanks for the heads up. – Eamorr Feb 18 '11 at 17:14
1

I don't know what you mean with "static class", but what you want is a singleton. See this question for various methods on how to set one up.

Community
  • 1
  • 1
DarkDust
  • 90,870
  • 19
  • 190
  • 224