title: iOS Core data update to data not getting saved
body: <p>I'm using core data entity named <code>CrumbPoint</code> which stores latitude and longitude and it points to (has relation to) another entity named <code>HRRecord</code>. CrumbPoint is created this way:</p>
  
  <pre><code>CrumbPoint *crumbPoint = [NSEntityDescription insertNewObjectForEntityForName:@"CrumbPoint"
                                                         inManagedObjectContext:context];
  crumbPoint.lat = [NSNumber numberWithFloat:lat];
  crumbPoint.lon = [NSNumber numberWithFloat:lon];
  crumbPoint.velocity = [NSNumber numberWithFloat:velocity];
  crumbPoint.date = [NSDate date];
  
  // Since this use search query, always refresh from DB
  // The following fetch using NSFetchRequest when the record is available.
  HrRecord * hr = [HRRecord hrRecordWithTitle:title inManagedObjectContext:context];
  crumbPoint.inRecord = hr;
  </code></pre>
  
  <p>HrRecord has a field named <code>distance</code> which is what I need to update whenever the device has a location update. (I am keeping track of user jogging). For every location update, a new <code>CrumbPoint</code> is created, and it points to the same <code>HrRecord</code> for the same jogging session. The new distance between previous location and the new location needs to be calculated and the distance for the <code>HrRecord</code> needs to be updated.</p>
  
  <p>However my problem is that every time I'm getting the HrRecord (Maybe this is a bad design, but I'm using <code>NSFetchRequest</code> to query for the <code>HrRecord</code> each time for a new location update.)</p>
  
  <p>Now when I try to update:</p>
  
  <pre><code>HrRecord * hr = crumbPoint.inRecord;
  float oldDistance = [hr.distance doubleValue];
  // code to calculate distance here, then, update
  hr.distance = [NSNumber numberWithDouble: newDistanceUpdate];
  </code></pre>
  
  <p><code>hr.distance</code> would always be 0 before update, even though every time after the update I can print out new value. I tried sending <code>save</code> to managed object context but it doesn't seem to work either. Why is that?</p>
  
  <p>EDIT:
  Here is the code to inserting. Maybe the bug is not about saving, after the context is saved I tried pulling out the record with <code>[HrRecord HrRecordWithTitle:title inManagedObjectContext:context]</code> and distance IS updated for that round. But next time the location update comes in it's 0 again for some reason. I gotta check for more code. :/</p>
  
  <pre><code>+(HrRecord *)HrRecordWithTitle: (NSString *)title
                        inManagedObjectContext:(NSManagedObjectContext *)context
  {
      HrRecord * record = nil;
      NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"HrRecord"];
      request.predicate = [NSPredicate predicateWithFormat:@"title = %@", title];
      NSSortDescriptor * sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
      request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
  
      NSError * error = nil;
      NSArray * records = [context executeFetchRequest:request error:&amp;error];
  
      if (!records || records.count &gt; 1) {
          // Nil, or more than one is an error
      } else if (records.count == 0) {
          // Create a new record with starting duration of 0
          record = [NSEntityDescription insertNewObjectForEntityForName:@"HrRecord" inManagedObjectContext:context];
          record.title = title;
          record.duration = @0.0;
          record.distance = @0.0;
          record.date = [NSDate date];
      } else { // Recrod exists, exactly one
          record = [records lastObject];
          // update duration
          record.duration = @([record.duration intValue] + 1);
      }
  
      return record;
  }
  </code></pre>