【问题标题】:How to save data from reversegeocoding to CoreData in Objective-C?如何在 Objective-C 中将数据从反向地理编码保存到核心数据?
【发布时间】:2015-12-14 20:55:56
【问题描述】:

我正在尝试对当前位置进行反向地理编码,检索城市和州并将其保存到 CoreData 实体。在下面的代码中,一旦用户点击保存按钮,我希望进行反向地理编码并将城市和州信息保存到 Core Data。但是当我尝试执行下面的代码时,没有执行反向地理编码部分,也没有数据保存到核心数据中。我在其他线程中发现的一个指针是反向地理编码发生在一个独立的线程中,并且该函数的稍后部分在没有发生反向地理编码的情况下被启动。有人可以指导我如何解决这个问题吗?我还是iOS应用程序开发的新手,所以非常感谢详细的回复。提前致谢!

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

【问题讨论】:

  • reverseGeocodeLocation 是一个异步调用。如果您需要对 placemark 做一些事情,请将代码移动到完成处理程序中。
  • 我尝试按照 DeveloBär 在他的回答中建议的操作,但遇到了我的评论中描述的问题。

标签: ios objective-c core-data reverse-geocoding reversegeocodelocation


【解决方案1】:

试试这个:

- (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 秒)。 如果您在收到数据之前尝试保存数据,您确实不会存储任何内容。

【讨论】:

  • 感谢您的回复。你的解决方案很有意义,我试过了,但是当我这样做时,当我点击“保存”按钮时没有任何反应。基本上完成处理程序仍然没有被执行。我在NSManagedObject *newPlace = [NSEntityDescription insertNewObjectForEntityForName:@"Place" inManagedObjectContext:context]; 放置了一个断点,但这个断点永远不会被命中。虽然我确实在 NSlog 中看到“解析地址”,这证明它只是被跳过的完成处理程序代码。我是否需要做任何其他事情来触发完成处理程序代码?
猜你喜欢
  • 2011-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 2016-08-19
  • 2015-01-10
相关资源
最近更新 更多