SAHM
SAHM

Reputation: 4179

Most Efficient Alternative Method of Storing Settings for iPhone Apps

I am not using the Settings bundle to store the settings for my app, as I prefer to allow the user to access the settings within the app (they may be changed fairly often). I do realize that there is the option to do both, but for now, I am trying to find the most optimal place to store the settings within the app.

I have a good number of settings (from what I have read, probably too many for NSUserDefaults), and the two main options I am considering are: 1) storing the settings in a dictionary in the plist, loading the settings into a NSDictionary property in the app delegate and accessing them via the sharedDelegate 2) storing the settings in a Core Data entity (1 row on Settings entity), loading the settings into a Settings object in the app delegate and accessing them via the sharedDelegate

Of these two, which would be the optimal method, performance wise?

Upvotes: 0

Views: 190

Answers (2)

Janak Nirmal
Janak Nirmal

Reputation: 22726

You should go with NSUserDefaults only. If you think there are lot of NSUserDefaults I Suggest you to create 1 model class and go with it. Create one model class and have your settings as properties and create object of that class and save it to NSUserDefaults will be easy for you as it is one time implementation only.

@interface SettingsModelClass : NSObject 
{
    NSString *strSetting1;
    BOOL boolSetting2;
}

@property (nonatomic,retain) NSString *strSetting1;
@property (nonatomic,assign) BOOL boolSetting2;

//Implementaiton

@implementation SettingsModelClass

@synthesize strSetting1, boolSetting2;

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

- (void)encodeWithCoder:(NSCoder *)encoder
{
    //Encode properties, other class variables, etc
    [encoder encodeObject:self.strSetting1 forKey:@"StringSetting"];    
    [encoder encodeBool:self.boolSetting2 forKey:@"BoolSetting"];
}

- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if( self != nil )
    {
        //decode properties, other class vars
        self.strSetting1 = [decoder decodeObjectForKey:@"StringSetting"]; 
        self.boolSetting2 = [decoder decodeBoolForKey:@"BoolSetting"]; 
    }
    return self;
}

@end

Please leave comments if you have any doubt. I would love to help you.

Update Yes you would access through application delegate. Or even you can create it as singleton class instance. This approach is using OOPS fundas so it is batter to follow this pattern that I believe.

Upvotes: 1

sosborn
sosborn

Reputation: 14694

Just use NSUserDefaults, that is what it is there for. Why do you feel like you have too many for it?

Also, if you decide to use the settings bundle later, you'll need to use NSUserDefaults anyway.

Upvotes: 3

Related Questions