Search code examples
objective-cparse-platformpfquery

Am I making two queries?


Since PFFiles can only retrieved through getDataInBackgroundWithBlock because they're not objects (I think..) after making a query I'm calling getDataInBackgroundWithBlock to convert PFFile into UIImage.

Am I making two queries by calling findObjectInBackgroundWithBlock and then getDataBackgroundWithBlock? If so, is there a way to get this done with one query only?

PFQuery *query = [PFQuery queryWithClassName:@"photoObject"];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){

        for (id object in objects) {


            PFFile *imageFile = object[@"photo"];
            [imageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
                if (!error) {

                    [self.annotationArray addObject:object[@"location"]];
                    NSLog(@"Annotation's coordinate : %@", self.annotationArray);


                    self.callOutImage = [UIImage imageWithData:imageData];

                    self.geoPoint = object[@"location"];

                    CLLocationCoordinate2D locCoord = CLLocationCoordinate2DMake(self.geoPoint.latitude, self.geoPoint.longitude);
                    CustomPin *pin = [[CustomPin alloc] initWithTitle:[object objectId] location:locCoord image:self.callOutImage];
                    [self.mainMap addAnnotation:pin];

                }}];

        }

    }];

Solution

  • You're making two asynchronous network requests, but only one of the requests -- findObjects -- is a query. The find returns an object that points to a file, and in order to get the file's contents, a second (non-query) request is required.

    There is a way to do what you're doing with a single request from the client: a cloud function that does essentially the same thing the posted code, and then returns the retrieved data.