How do you store data from NSMutable Array in Core Data?
Приложение, которое я делаю, рисуетPolyline
на основе пользовательских координат отCLLocationManager
(все они хранятся вNSMutableArray
)
Когда приложение закрывается, текущая полилиния исчезает. Как я могу сохранить массив точек в CoreData для извлечения при запуске приложения? Все прошлые «маршруты», пройденные пользователем с момента запуска приложения, должны быть восстановлены и наложены на карту (что может быть довольно много, поэтому CoreData кажется лучшим вариантом из того, что я собрал).
Это код, который я использую для создания MKPolyline из загруженных координат
-(IBAction)didClickLoadCoordinates:(id)sender {
// get a reference to the appDelegate so you can access the global managedObjectContext
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Route"];
NSError *error;
id results = [appDelegate.managedObjectContext executeFetchRequest:request error:&error];
if ([results count]) {
polyLine = (Route *)(results[0]);
NSArray *coordinates = polyLine.coordinates;
int ct = 0;
for (CLLocation *loc in coordinates) {
NSLog(@"location %d: %@", ct++, loc);
}
// this copies the array to your mutableArray
_locationsArray = [coordinates mutableCopy];
}
NSInteger numberOfSteps = _locationsArray.count;
//convert to coordinates array to construct the polyline
CLLocationCoordinate2D clCoordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [_locationsArray objectAtIndex:index];
CLLocationCoordinate2D coordinate2 = location.coordinate;
clCoordinates[index] = coordinate2;
}
MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:clCoordinates count:numberOfSteps];
[_mapView addOverlay:routeLine];