私が理解していなかったのはこれでした: FetchRequest を実行して、パスが存在するかどうかを確認する必要があります。
- (NSArray *)checkIfPathExistsByName {
// check first if this path exists ..
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Path" inManagedObjectContext:context];
[request setEntity:entityDescription];
// Set WHERE clause
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(name LIKE %@)", currentPathName];
[request setPredicate:predicate];
// sort
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"createdDate" ascending:NO];
[request setSortDescriptors:@[sortDescriptor]];
NSError *error;
NSArray *array = [context executeFetchRequest:request error:&error];
return array;
これを locationManager デリゲートで呼び出します
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
// check first if this path exists ..
NSArray *array;
array = [self checkIfPathExistsByName];
if (array == nil)
{
// Deal with error...
}
else
{
if (array.count == 0){
// if exists, just add Locations
[self startNewPathAndInsertFirstCoordinate:newLocation forPathName:currentPathName];
}else{
// start new if doesn't exist
Path *myPath = [array lastObject];
[self insertNewCoordinate:newLocation forPath:myPath];
}
}
ここで、新しいパスの場合は currentPathName と場所を startNewPathAndInsertFirstCoordinate に渡しますが、パス (array.count>0) が見つかった場合は、配列からパスを取り出して InsertNewCoordinate という別のメソッドに渡します。パスを取得したため、insertNewCoordinate では、次のように挿入時に位置とパスの関係を設定できます。
- (void)insertNewCoordinate:(CLLocation *)newLocation forPath:(Path *)currentPath{
//SEND TO CORE DATA
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
NSLog(@"howRecent: %f\n",howRecent);
// get GPS lat/lng
double lat = newLocation.coordinate.latitude;
double lng = newLocation.coordinate.longitude;
// insert location/GPS point
Location *currentPathLocation = [NSEntityDescription insertNewObjectForEntityForName:@"Location" inManagedObjectContext:context];
currentPathLocation.lat = [NSNumber numberWithDouble:lat];
currentPathLocation.lng = [NSNumber numberWithDouble:lng];
// insert 'Location' relationship to 'Path'
[currentPath addLocationObject:currentPathLocation];
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}}
- (void)startNewPathAndInsertFirstCoordinate:(CLLocation *)newLocation forPathName:(NSString *)pathName{
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
NSLog(@"howRecent: %f\n",howRecent);
if (abs(howRecent) < 35.0)
{
//Location timestamp is within the last 15.0 seconds, let's use it!
NSLog(@"horizontalAccuracy: %f\n",newLocation.horizontalAccuracy);
if(newLocation.horizontalAccuracy < 85.0){
//Location seems pretty accurate, let's use it!
NSLog(@"latitude %+.6f, longitude %+.6f\n",
newLocation.coordinate.latitude,
newLocation.coordinate.longitude);
NSLog(@"Horizontal Accuracy:%f", newLocation.horizontalAccuracy);
//SEND TO CORE DATA
// get GPS lat/lng
double lat = newLocation.coordinate.latitude;
double lng = newLocation.coordinate.longitude;
// insert Path
Path *currentPathInfo = [NSEntityDescription insertNewObjectForEntityForName:@"Path" inManagedObjectContext:context];
currentPathInfo.name = currentPathName;
currentPathInfo.createdDate =[NSDate date];
// insert location/GPS point
Location *currentPathLocation = [NSEntityDescription insertNewObjectForEntityForName:@"Location" inManagedObjectContext:context];
currentPathLocation.lat = [NSNumber numberWithDouble:lat];
currentPathLocation.lng = [NSNumber numberWithDouble:lng];
// insert 'Location' relationship to 'Path'
[currentPathInfo addLocationObject:currentPathLocation];
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
}
}
[self.locationManager startUpdatingLocation];
}