user3029309
user3029309

Reputation: 11

Objective C - Parse.com Save and Read Geolocation IOS

i want to Read the Geolocation from the Parse Database and show the Location from another Users in the Map.

I Write the Location to the parse Database :

 [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
    if (!error) {




       [[PFUser currentUser] setObject:geoPoint forKey:@"currentLocation"];
        [[PFUser currentUser] saveInBackground];

And then i read the Usernames and the Locations

     PFQuery *User = [PFUser query];
    [User selectKeys:@[@"username"]];
    [User findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {


        Name = results;


              }];

    PFQuery *Location = [PFUser query];
    [Location   selectKeys:@[@"currentLocation",]];
    [Location findObjectsInBackgroundWithBlock:^(NSArray *location, NSError *error)
     {


        Coordinaten = location;

 }];




    NSLog(@" %@ hat die %@",Name , Coordinaten);
   // ann.title = [Name objectAtIndex:1] ;
   // ann.coordinate = [[Coordinaten objectAtIndex:1] coordinate];
  //  [MapView addAnnotation:ann];

The first Problem is, the NSLog show nothing . When I copy the NSLog under the Coordinaten = Location , they show me only the Values for Coordinaten. Under Name = results only the Names .

NSArray Coordinaten / Names are decelerated in the .h

The Next thing is, how i set the title and the coordinate with these NSArrays

 // ann.title = [Name objectAtIndex:1] ;
   // ann.coordinate = [[Coordinaten objectAtIndex:1] coordinate];
  //  [MapView addAnnotation:ann];

Thanks!

Upvotes: 0

Views: 1490

Answers (1)

Timothy Walters
Timothy Walters

Reputation: 16884

This is yet another case of not understanding the nature of asynchronous programming.

Consider this situation:

You want to make an egg sandwich. You put the eggs on to boil, and set an alarm for when they're cooked to get them out, peel them, cut them up and add them to your sandwich. While you wait you get the bread and butter it, then wait for the alarm to go off.

Your call to findObjectsInBackgroundWithBlock is putting the eggs on to boil. The block you pass it is the alarm and what you plan to do with the eggs once cooked.

Your code above is akin to putting the eggs on to boil, then straight away trying to use the uncooked/partially-cooked eggs on your sandwich. They may or may not have cooked enough by then. Usually it makes a big mess.

One solution is to call a method at the end of the block your pass to the method.

Upvotes: 6

Related Questions