1

I am pasring json data from a url and taking one of teh values to NSUserdefaults to use it it application view .

Example user will enter an unique code given to him and this code will be appended to the requested URL , accoridng to the requested code the json value will be changed . At present when i enter code it is fetching and saving NSuserdefaults and passing it to the label filed in view . But when i enter new code and fecth new data it is not updating in the view . If i restart the application and enter new code then it is showing new Value . Can somebody help me . here is the code

NSString *jsonUrlString = [[NSString alloc] initWithFormat:@"http://mywebsite.com/json/code.php?user=%@",_opcodeField.text];
NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
[_indicator startAnimating];
_indicator.hidesWhenStopped = YES;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    //-- JSON Parsing
   NSMutableArray *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
   NSLog(@"settings = %@",result);

    for (NSDictionary *dic in result)
    {
            [[NSUserDefaults standardUserDefaults] setObject:[dic objectForKey:@"footer"] forKey:@"op_footer"];
            [[NSUserDefaults standardUserDefaults] synchronize];

       [_indicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];
    }

View code is:

(void)viewDidLoad {
    [super viewDidLoad];
    _Footer.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"op_footer"];

How can I show new values without restarting application . Is there any code to be added in view ? I can see json is fetching correctly newvalues when user entered new code

Thank you.

4 Answers4

0

Are you sure viewDidLoad is the best place to add this code?.

Try to add your code in some method which is called more often. For debugging I usually add some button just to call a NSLog to see what happens.

Marcal
  • 1,371
  • 5
  • 19
  • 37
0

Try this if you want to load the text all the time you come to the viewcontroller.

- (void)viewWillAppear:(BOOL)animated {
    _Footer.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"op_footer"];
}

Or you can try this if you want to load it to a label after getting the new code.

- (void)updateLabel {
    _Footer.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"op_footer"];
}

Hope this helps.. :)

Rashad
  • 11,057
  • 4
  • 45
  • 73
0

The user defaults plist can take time to save and update. It doesn't happen instantly. Try triggering the call after a pause of some kind. Test with a button.

Edit: you can add an observer to the default so that you know it has updated - Cocoa - Notification on NSUserDefaults value change?

Community
  • 1
  • 1
sketchyTech
  • 5,746
  • 1
  • 33
  • 56
0

Why not move this line...

_Footer.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"op_footer"];

to here (unless of course this is in a different class)...

NSMutableArray *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"settings = %@",result);

for (NSDictionary *dic in result)
{
        [[NSUserDefaults standardUserDefaults] setObject:[dic objectForKey:@"footer"] forKey:@"op_footer"];
        [[NSUserDefaults standardUserDefaults] synchronize];

   [_indicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];
}

//Update it straight away
_Footer.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"op_footer"];

Also bit confused why you are looping through dictionaries here? Is there more than one dictionary? If there is the last value will always be the one that gets stored in UserDefaults.

You are also stopping the animation in the loop as well?

Flexicoder
  • 8,251
  • 4
  • 42
  • 56
  • Hi i moved animation from loop . thanks for that . View class is different so i cannot update in the json fetching class . There are 2 views in the middle 1. User will enter a code ( 1st view) here we will fetch settings 2. another view which shows authentication details ( still we have fecthed values in NSuserdefaults but not used here) 3. 3rd view iwll show the value fetched ( here we will pull the NSdefaults) – sreekanth balu Apr 02 '14 at 09:43