title: NSUserDefaults save and reload while using app but reset when app is restarted
body: <p>I have a method to increment a counter if it is the first time a user has been messaged on the particular day. Code below:</p>
  
  <pre><code>//here we decide if to increment it or not
  -(BOOL)canIncrementCountForUser: (NSString *)user {
  
      //erase the dictionary if it's a new day
      [self flushDictionaryIfNeeded];
  
      //load up a dictionary
      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
      NSMutableDictionary *dictionary = [defaults objectForKey:@"uniqueSentToday"];
  
      NSLog(@"%@", [dictionary allKeys]);
  
      //if empty it's a yes
      if([dictionary count]==0){
          NSLog(@"empty dictionary");
          NSLog(@"First message for %@ today!",user);
          NSDate *now = [NSDate date];
          [dictionary setObject:now forKey:user]; //do I need to set it back again?
          [defaults synchronize];
          return YES;
  
      }
  
      //if it's not empty it's only a yes if the key doesn't exist
      else {
  
          //not in dict so unique
          if(![dictionary objectForKey:user]){
              NSLog(@"First message for %@ today!",user);
              NSDate *now = [NSDate date];
              [dictionary setObject:now forKey:user]; //do I need to set it back again?
              [defaults synchronize];
              return YES;
  
          }
  
  
          else {
  
              NSLog(@"Already messaged %@ today!",user);
              return NO;
  
  
  
          }
  
  
  
  
      }
  
  
  }
  
  -(void)flushDictionaryIfNeeded{
  
      //set dictionary
      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
      NSMutableDictionary *dictionary = [defaults objectForKey:@"uniqueSentToday"];
  
      if([dictionary count]&gt;0) {
  
          //get any date
          NSDate *aDate = nil;
          NSArray *values = [dictionary allValues];
          aDate = [values objectAtIndex:0];
  
          NSDateComponents *otherDay = [[NSCalendar currentCalendar] components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:aDate];
          NSDateComponents *today = [[NSCalendar currentCalendar] components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
          if([today day] == [otherDay day] &amp;&amp;
             [today month] == [otherDay month] &amp;&amp;
             [today year] == [otherDay year] &amp;&amp;
             [today era] == [otherDay era]) {
  
              NSLog(@"Don't flush");
          }
  
          else {
  
              NSLog(@"It's a new day! erase dictionary!");
              [dictionary removeAllObjects];
              [defaults synchronize];
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>If a user is messaged a NSDate object will be created with the user's username as the key. If a key doesn't exist the counter can be incremented and the key is added, if it does exist the method returns false. I also have a method to erase all the contents if it's a new day. The code is all working fine and it seems like the NSMutableDictionary is being saved to the application fine when the program is being used but when I restart the app the dictionary will be empty. Can someone give me a pointer on why this might be happening? Thanks</p>