views:

64

answers:

1

Been a tough week, will try to make this as clear as possible. Appreciate you taking the time to read.

Hi, I'm sending a registration request to a server. A user object in jSon is returned, I parse the jSon and save the jSon dictionary phone as a method on my User class. However, whenever I load the user from the phone - by reading the saved dictionary, then converting that into a user object - I am getting an old user object, one that I saved last week.

Whenever I receive the jSon for the user, I make several log statements for their SAT (Single Access Token). The first two single access token logs show the new one, generated by the server. The third log statement shows the old SAT from a previous User.

From this I can conclude that saving to the phone is working incorrectly. Inside my userFromDictionary method, I call saveUserToPhone. The logic here is that a new dictionary is provided from the server's jSon, so we should go ahead and save this new dictionary to the phone for future loading of the User.

User loading/saving logic:
        self.user = [User userFromDictionary:userProperties];   
    NSLog(@"RegistrationScreenOne: User SAT: %@",self.user.singleAccessToken);
    [User saveUserToPhone:userProperties];
    NSLog(@"RegistrationScreenOne: User SAT: %@",self.user.singleAccessToken);      
    self.user = [User userFromPhone];
    NSLog(@"RegistrationScreenOne: User SAT: %@",self.user.singleAccessToken);

--The first 2 Logs show the new SAT, the third log shows an old one.

All of this is inside the Simulator. I shutdown iPhone Simulator and relaunch from Xcode whenever I run.

User implementation file:

+(NSDictionary *)jSonMapping{
return [NSDictionary dictionaryWithObjectsAndKeys:
        @"username", @"username",
        @"password", @"password",
   ////
   ////ETC, spared for length
   ////     
 }

+(NSString *)userFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilestats];
}

   ////
   ////kFileStats is declared in the header as @"user-file.plist"
   ////


+(User*)userFromDictionary:(NSDictionary *)dictionary{

NSDictionary *mapping = [self jSonMapping];

User *user = [[User alloc] init];

for (NSString *attribute in [mapping allKeys]){
    NSString *classProperty = [mapping objectForKey:attribute];
    NSString *attributeValue = [dictionary objectForKey:attribute];
    [user setValue:attributeValue forKeyPath:classProperty];
    NSLog(@"Class Property: %@ Value: %@",classProperty,attributeValue);
}   

[self saveUserToPhone:dictionary];

return [user autorelease];
}

+(BOOL)doesUserExistOnPhone{
NSString *dataPath = [self userFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
    return YES;
}

return NO;
}

+(User*)userFromPhone{
User *user = [[User alloc] init];

NSString *dataPath = [self userFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]){

    NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:dataPath];      
    user = [self userFromDictionary:dictionary];
}

return user;
}

+(void)saveUserToPhone:(NSDictionary *)dictionary{
[dictionary writeToFile:[self userFilePath] atomically:YES];    
 }

Log output from those 3 log statements, including going over the properties:

First my new user, I used random number strings for their data:
2010-09-08 14:29:05.274 AppNameWindowedApp[6873:207] Class Property: lastName Value: 2309320923
2010-09-08 14:29:05.274 AppNameWindowedApp[6873:207] Class Property: firstName Value: 309209239032
2010-09-08 14:29:05.275 AppNameWindowedApp[6873:207] Class Property: schoolName Value: ##################
2010-09-08 14:29:05.275 AppNameWindowedApp[6873:207] Class Property: mobile Value: 23902390
2010-09-08 14:29:05.276 AppNameWindowedApp[6873:207] Class Property: singleAccessToken Value: xUd3-OfcUrEaHf3FIKdl
2010-09-08 14:29:05.277 AppNameWindowedApp[6873:207] Class Property: password Value: (null)
2010-09-08 14:29:05.278 AppNameWindowedApp[6873:207] Class Property: email Value: [email protected]
2010-09-08 14:29:05.278 AppNameWindowedApp[6873:207] Class Property: schoolID Value: 4c871745a1e7490b4d000008
2010-09-08 14:29:05.279 AppNameWindowedApp[6873:207] Class Property: role Value: admin
2010-09-08 14:29:05.279 AppNameWindowedApp[6873:207] Class Property: username Value: 23903902

The Single Access Token after setting a user from the jSon dictionary:

2010-09-08 14:29:05.280 AppNameWindowedApp[6873:207] RegistrationScreenOne: User SAT: xUd3-OfcUrEaHf3FIKdl

The second Log statement for Single Access Token, after saving:

2010-09-08 14:29:05.280 AppNameWindowedApp[6873:207] RegistrationScreenOne: User SAT: xUd3-OfcUrEaHf3FIKdl

The user class properties when reading from the phone:

2010-09-08 14:29:05.281 AppNameWindowedApp[6873:207] Class Property: lastName Value: Admin
2010-09-08 14:29:05.282 AppNameWindowedApp[6873:207] Class Property: firstName Value: Admin
2010-09-08 14:29:05.282 AppNameWindowedApp[6873:207] Class Property: schoolName Value: #############
2010-09-08 14:29:05.283 AppNameWindowedApp[6873:207] Class Property: mobile Value: 
2010-09-08 14:29:05.283 AppNameWindowedApp[6873:207] Class Property: singleAccessToken Value: RL01Mv1-yJacB_FAxHG1
2010-09-08 14:29:05.283 AppNameWindowedApp[6873:207] Class Property: password Value: (null)
2010-09-08 14:29:05.284 AppNameWindowedApp[6873:207] Class Property: email Value: [email protected]
2010-09-08 14:29:05.286 AppNameWindowedApp[6873:207] Class Property: schoolID Value: 4c86f53ff7c9ff494f000001
2010-09-08 14:29:05.286 AppNameWindowedApp[6873:207] Class Property: role Value: AppName
2010-09-08 14:29:05.286 AppNameWindowedApp[6873:207] Class Property: username Value: admin

The Single Access Token after reading UserFromPhone:

2010-09-08 14:29:05.287 AppNameWindowedApp[6873:207] RegistrationScreenOne: User SAT: RL01Mv1-yJacB_FAxHG1

A: 

It's possible that the file is not being overwritten, which would explain why you're getting an old record.

Ben Scheirman
There was a null value in the dictionary it tried to save. Thanks!
quantumpotato