Eugene Trapeznikov
Eugene Trapeznikov

Reputation: 3240

Global variable in iOS TabBar Application

I am creating ios app in xcode 4.2. I have outside file with database. I dont wanna download data in every view. How should i create one global variable for tabbar application? And when should i upload this database before closing of application?

Upvotes: 4

Views: 2079

Answers (3)

Eugene Trapeznikov
Eugene Trapeznikov

Reputation: 3240

I use singletones like this: in class DataBase with some arrays of data i implement share method:

+(id)share
{
    static id share = nil;

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

    return share;
}

and then in some classes: self.dataBase = [DataBase share];

Upvotes: 2

Popeye
Popeye

Reputation: 12093

You can create global variables by doing this

extern NSString *someString;

@interface ......

@property (strong, nonatomic) NSString *someString;

@end

@implementation ......

@systhesize someString;

NSString *someString;

@end

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726559

In iOS applications the model data is often kept in a singleton, rather than in a global variable. Here is an article briefly describing singletons in Objective-C.

You can load your data in the class method that initializes your shared singleton. Uploading the data back is a bit trickier, because the singleton itself does not know when to do it. Therefore you should make an instance method -(void)uploadData in your singleton class, and call that method when your application is about to close. applicationWillResignActive: method of your application delegate is a good place to initiate the upload.

Upvotes: 5

Related Questions