Friday, 15 June 2012

ios - NSUserDefaults clears its value after specific time -



ios - NSUserDefaults clears its value after specific time -

i save dictionary using nsuserdefaults follow:

nsdata *data = [nskeyedarchiver archiveddatawithrootobject:dictionary]; [[nsuserdefaults standarduserdefaults] setobject:data forkey:@"key"]; [[nsuserdefaults standarduserdefaults] synchronize]; // unarchive nsdata *newdata = [[nsuserdefaults standarduserdefaults] objectforkey:@"key"]; nsdictionary *newdict = [nskeyedunarchiver unarchiveobjectwithdata:data];

problem: dicitionary contains name value other values, after specific period of time (mostly after 24 hours), name values gives me " "(blank) value.

i see kind of issue first time, help me solve this. give thanks you.

the problem not in pice of code.

1) seek check if application contains [[nsuserdefaults standarduserdefaults] setobject:data forkey:@"key"] in classes/methods.

2) check if nsdata *data = [nskeyedarchiver archiveddatawithrootobject:dictionary]; not empty when writing (maybe get's corrupted, , wipes written data). check issue can create little versioning , debug issue, here code.

nsmutablearray *datas = [[[nsuserdefaults standarduserdefaults] objectforkey:@"datas"] mutablecopy]; if (!datas) { datas = [nsmutablearray new]; } nsdata *data = [nskeyedarchiver archiveddatawithrootobject:dictionary]; [datas addobject:data]; [[nsuserdefaults standarduserdefaults] setobject:datas forkey:@"datas"]; [[nsuserdefaults standarduserdefaults] synchronize]; // debug code see result nslog(@"======= logging saved datas"); (nsdata *data in datas) { nsdictionary *dict = [nskeyedunarchiver unarchiveobjectwithdata:data]; nslog(@"%@", dict); }

this code allow see versions of dictionaries saved in nsuserdefaults, , moment when empty info saved.

ios objective-c iphone nsuserdefaults

No comments:

Post a Comment