Wie speichert man Daten von NSMutable Array in Core Data?

Die App, die ich mache, zeichnet einPolyline basierend auf den Benutzerkoordinaten vonCLLocationManager (diese werden alle in einem aufbewahrtNSMutableArray)

Wenn die App geschlossen wird, verschwindet die aktuelle Polylinie. Wie kann ich das Punktearray in CoreData speichern, das beim Start der App abgerufen werden soll? Alle bisherigen "Routen", die der Benutzer seit dem Start der App genommen hat, sollten wiederhergestellt und auf der Karte überlagert werden (was sehr viel sein kann, sodass CoreData die beste Option für das ist, was ich gesammelt habe).

Dies ist der Code, den ich verwende, um aus den geladenen Koordinaten eine MKPolyline zu erstellen

-(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];

Antworten auf die Frage(1)

Ihre Antwort auf die Frage