0

我正在尝试使用当前位置进行反向地理编码,检索City和State并将其保存到CoreData实体。在下面的代码中,只要用户点击保存按钮,我希望反向地理编码发生,并将城市和州的信息保存到核心数据。但是当我尝试执行下面的代码时,反向地理编码部分不会执行,也不会将数据保存到Core Data。我在其他线程中发现的一个指针是反向地理编码发生在独立线程中,稍后部分此功能会被踢入,而不会发生反向地理编码。有人能指导我如何解决这个问题吗?我还是iOS应用程序开发的新手,非常感谢您的详细回复。提前致谢!如何在Objective-C中将反向地理编码数据保存到CoreData中?

- (IBAction)save:(id)sender { 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    //Update current location 
    [self.locationManager startUpdatingLocation]; 

    // Reverse Geocoding 
    NSLog(@"Resolving the Address"); 
    [geocoder reverseGeocodeLocation:self.locationManager.location completionHandler:^(NSArray *placemarks, NSError *error) 
    { 
     NSLog(@"Found placemarks: %@, error: %@", placemarks, error); 
     if (error == nil && [placemarks count] > 0) { 
      placemark = [placemarks lastObject]; 
     } else { 
      NSLog(@"%@", error.debugDescription); 
     } 
    } ]; 

    NSManagedObjectContext *context = [self managedObjectContext]; 

    // Create a new device 
    NSManagedObject *newPlace = [NSEntityDescription insertNewObjectForEntityForName:@"Place" inManagedObjectContext:context]; 
    [newPlace setValue:placemark.locality forKey:@"city"]; 
    [newPlace setValue:placemark.administrativeArea forKey:@"state"]; 

    NSError *error = nil; 
    // Save the object to persistent store 
    if (![context save:&error]) { 
     NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); 
    } 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
+1

'reverseGeocodeLocation'是一个异步调用。如果您需要使用'placemark'做些事情,请将代码移到完成处理程序中。 – Sulthan

+0

我尝试着在开发者的答案中做了以下建议,但是在我的评论中遇到了一个问题,如下所述。 – hellSigma

回答

0

试试这个:

- (IBAction)save:(id)sender { 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    //Update current location 
    [self.locationManager startUpdatingLocation]; 

    // Reverse Geocoding 
    NSLog(@"Resolving the Address"); 
    [geocoder reverseGeocodeLocation:self.locationManager.location completionHandler:^(NSArray *placemarks, NSError *error) 
    { 
     NSLog(@"Found placemarks: %@, error: %@", placemarks, error); 
     if (error == nil && [placemarks count] > 0) { 
      placemark = [placemarks lastObject]; 

      NSManagedObjectContext *context = [self managedObjectContext]; 

      // Create a new device 
      NSManagedObject *newPlace = [NSEntityDescription insertNewObjectForEntityForName:@"Place" inManagedObjectContext:context]; 
      [newPlace setValue:placemark.locality forKey:@"city"]; 
      [newPlace setValue:placemark.administrativeArea forKey:@"state"]; 

      NSError *error = nil; 
      // Save the object to persistent store 
      if (![context save:&error]) { 
       NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); 
      } 

      [self dismissViewControllerAnimated:YES completion:nil]; 

     } else { 
      NSLog(@"%@", error.debugDescription); 
     } 
    }]; 

} 

正如你可以看到我把代码保存反馈回调中的数据。 回调是异步的,在信息变得可用之后调用(这可能需要时间;例如2秒)。 如果您在收到数据之前尝试保存数据,则确实不会存储任何内容。

+0

感谢您的回复。你的解决方案是有道理的,我尝试过,但当我这样做时,没有任何反应,当我点击“保存”按钮。基本上完成处理程序仍然没有得到执行。我在'NSManagedObject * newPlace = [NSEntityDescription insertNewObjectForEntityForName:@“Place”inManagedObjectContext:context]中放置了一个断点;'但是这个断点永远不会被命中。虽然我确实在NSlog中看到“解析地址”,证明它只是跳过的完成处理程序代码。我是否需要做其他事情来触发完成处理程序代码? – hellSigma