Wednesday, September 21, 2011

Making sure default prefs persist in iOS

Wait, I've suddenly lost my saving or retrieval of my settings. Let's take a look at the code.

Yeah, it's saving it:



[prefs setInteger:jlptLevel forKey:@"jlptLevel"];

[prefs setInteger:startNumInt forKey:@"startNum"];

[prefs setInteger:endNumInt forKey:@"endNum"];

// saving it all
[prefs synchronize];


But the question is, why isn't it persisting it between iterations of the iOS Simulator? I'm almost positive it was doing this before.

Maybe I should test it on my iTouch.

So this gives a way to check the path.

NSArray *path = NSSearchPathForDirectoriesInDomains(
NSLibraryDirectory, NSUserDomainMask, YES);
NSString *folder = [path objectAtIndex:0];
NSLog(@"Your NSUserDefaults are stored in this folder: %@/Preferences", folder)

But, why when I do synchronize doesn't it save it?

According to SO:

http://stackoverflow.com/questions/6033385/nsuserdefaults-not-being-saved-in-simulator



Your process is possibly terminated improperly so that NSUserDefaults do not have a chance to be stored. See also this and mostly this.

The suggestion in the second post I link to is to call synchronize in applicationDidEnterBackground:

Keep also in mind that terminating your app by stopping it in Xcode most often does not save user defaults.



Ok, well, there are a couple of problems. One is that the start num string is being dealt with in the on load, while the picker view is being dealt with the viewWillAppear. But, mostly - I'm not pulling the start an end number from prefs in either method. So, for now I'll just stick it where it's already mentioned.

Ok, here we go:

// Initialize start, end numbers

prefs = [NSUserDefaults standardUserDefaults];

startNumInt = [prefs integerForKey:@"startNum"];

if (startNumInt == 0){
startNumInt = 1;
}

NSString* startNumString = [NSString stringWithFormat:@"%d", startNumInt];

[startNum setText:startNumString];

startNumInt = [prefs integerForKey:@"endNum"];

if (endNumInt == 0){
endNumInt = 10;
}

NSString* endNumString = [NSString stringWithFormat:@"%d", endNumInt];

[endNum setText:endNumString];


So, all I had to do was add the retrieval. I must not have done it originally.

Hmm...that didn't work. Ok. Well, one problem is I was calling startInt endInt. So, I don't know if moving it to the viewWillAppear made a difference. Maybe not. But anyway, they're all in the same method now.



-(void) viewWillAppear: (BOOL) animated {


// retreive the stored level from prefs

prefs = [NSUserDefaults standardUserDefaults];

startNumInt = [prefs integerForKey:@"startNum"];


NSLog(@"startNum from prefs: %d ", startNumInt );

if (startNumInt == 0){
startNumInt = 1;
}

NSString* startNumString = [NSString stringWithFormat:@"%d", startNumInt];


[startNum setText:startNumString];

endNumInt = [prefs integerForKey:@"endNum"];

NSLog(@"endNum from prefs: %d ", endNumInt );

if (endNumInt == 0){
endNumInt = 10;
}

NSString* endNumString = [NSString stringWithFormat:@"%d", endNumInt];


[endNum setText:endNumString];


jlptLevel = [prefs integerForKey:@"jlptLevel"];


NSLog(@"jlptLevel from prefs: %d ", jlptLevel );



etc.

No comments:

Post a Comment