我不明白的是:我需要做一个 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];
}