0

ユーザーに位置情報を入力してから送信をクリックするように求めるビューコントローラーがあります。その場合、データはプレイス ディクショナリにスローされ、メソッドupdatePlaceDictionaryとによってジオコーディングされgeocodeます。[userListing saveInBackground]次に、オブジェクトをオンライン データベースに送信します。submitユーザーが情報を入力して [送信] をクリックしたときに呼び出されるメソッドと、メソッドを次にupdatePlaceDictionary示しますgeocode

- (void)submit{
    PFObject* userListing = [PFObject objectWithClassName:@"userListing"];

    [self updatePlaceDictionary]; 
    [self geocode];
    [userListing setObject:listingLocation forKey:@"location"];

    [userListing saveInBackground];
    [listings addObject:userListing];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)updatePlaceDictionary {
    [self.placeDictionary setValue:self.streetField.text forKey:@"Street"];
    [self.placeDictionary setValue:self.cityField.text forKey:@"City"];
    [self.placeDictionary setValue:self.stateField.text forKey:@"State"];
    [self.placeDictionary setValue:self.zipField.text forKey:@"ZIP"];

}

- (void)geocode{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressDictionary:self.placeDictionary completionHandler:^(NSArray *placemarks, NSError *error) {
        if([placemarks count]) {
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            CLLocation *location = placemark.location;
            CLLocationCoordinate2D coordinate = location.coordinate;
            listingLocation = [PFGeoPoint geoPointWithLatitude:coordinate.latitude longitude:coordinate.longitude]; 
        } else {
            NSLog(@"error");
        }
    }];    
}

3 つの方法はすべて問題なく機能します。問題は、submitメソッド内の次の行です。

[userListing setObject:listingLocation forKey@"location"]; 

キー「場所」に(0,0)の値を与えるだけです。これは、ジオコードが非同期で実行され、上記の行に到達するまでに終了しないために発生しています。ジオコードの実行が終了した後にこの値を設定するにはどうすればよいですか?


4

1 に答える 1