title: How to store MKOverlay object to file for retrieving later
body: <p>I am writing an iOS application to record your travel path and then storing it so that it can be retrieved later. Most of the code for drawing the path is based on the sample code <a href="http://developer.apple.com/library/ios/#samplecode/Breadcrumb/Introduction/Intro.html" rel="nofollow">Breadcrumb</a>. </p>
  
  <p>Now I am adding features to save the drawn overlay. What is best way to do it? I can use <code>CoreData</code> but I am not looking to do much to the drawn overlay rather than retrieving it later again. I have currently tried a simple <code>NSArray</code>. I convert the CrumbPath object into <code>NSData</code> and store it in the array. Later I retrieve it and convert it back to CrumbPath. However, I seem to be doing something wrong.</p>
  
  <pre><code>@interface CrumbPath : NSObject &lt;MKOverlay&gt;
  {
      MKMapPoint *points;
      NSUInteger pointCount;
      NSUInteger pointSpace;
  
      MKMapRect boundingMapRect;
  
      pthread_rwlock_t rwLock;
  }
  
  - (id)initWithCenterCoordinate:(CLLocationCoordinate2D)coord;
  - (MKMapRect)addCoordinate:(CLLocationCoordinate2D)coord;
  
  @property (readonly) MKMapPoint *points;
  @property (readonly) NSUInteger pointCount;
  
  @end
  </code></pre>
  
  <p>I save a CrumbPath object "crumbs" like this:</p>
  
  <pre><code>NSData *pointData = [NSData dataWithBytes:crumbs.points length:crumbs.pointCount * sizeof(MKMapPoint)];
  [patternArray addObject:pointData];
  [timeArray addObject:date];
  
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  // Create the full file path by appending the desired file name
  NSString *patternFile = [documentsDirectory stringByAppendingPathComponent:@"patterns.dat"];
  NSString *timeFile = [documentsDirectory stringByAppendingPathComponent:@"times.dat"];
  
  //Save the array
  [patternArray writeToFile:patternFile atomically:YES];
  [timeArray writeToFile:timeFile atomically:YES];
  </code></pre>
  
  <p>And retrieve it later like this to show in a table:</p>
  
  <pre><code>NSData *pointData = [appDelegate.patternArray objectAtIndex:indexPath.row];
  MKMapPoint *points = malloc(pointData.length);
  (void)memcpy([pointData bytes], points, sizeof(pointData));
  </code></pre>
  
  <p>And construct the path again:</p>
  
  <pre><code>crumbs = [[CrumbPath alloc] initWithCenterCoordinate:MKCoordinateForMapPoint(points[0])];
  for (int i = 1; i &lt;= pointCount; i++) {
      [crumbs addCoordinate:MKCoordinateForMapPoint(points[i])];
  }    
  [map addOverlay:crumbs];
  </code></pre>
  
  <p>However, I get an error: <code>'NSInvalidArgumentException', reason: '-[NSConcreteData isEqualToString:]: unrecognized selector sent to instance</code></p>