Refer to a Pointer type value in Swift with Parse

2

I have created in parse several classes and many of them related to each other by Pointers. An example is the following.

I have 2 classes:

  • subcategorias
  • comercios

In comercios I can have records of many trades, there is a field called come_subc_id and it is linked to class subcategorias by means of a Pointer.

In class subcategorias I have a classification of all shops, ie if it is a hotel, a restaurant, laundry, etc.

I have a PFQueryTableViewController where I intend to show a list of, for example, all hotels.

If in my class subcategorias for example the objectId of hotels is gL8qGBhXMI , how can I generate the query to show me only in the table all the hotels that I have registered in the business class? My question is, how can I generate a query of a class accessing the fields of another class through the pointer? I am trying it in the following way, but with errors and I have the following code and I still can not refine it and I do not know if I am going in the right direction.

override func queryForTable() -> PFQuery {
let queryHot = PFQuery(className: "comercios") //El que funciona
        queryHot.includeKey("come_subc_id") //Tipo pointer apuntando a clase subcategorias
        queryHot.whereKey("come_subc_id", equalTo: "gL8qGBhXMI") // comparando si el registro tiene clasificacion de hotel
return queryHot
}

When running the application I get the following error:

2016-01-24 14:05:35.579 AppBeta1[1045:35017] [Error]: pointer field come_subc_id needs a pointer value (Code: 102, Version: 1.11.0)

I understand that the error is generated in the query when I make the comparison:

queryHot.whereKey("come_subc_id", equalTo: "gL8qGBhXMI")

Since the field come_subc_id is not type string but it is an object, my question is about how to implement the correct method to make this comparison, if I can understand this part I can practically perform all the queries that are generated in my application since all classes are related by Pointers.

If I have not been entirely clear, I can rethink my problem.

I appreciate your support.

    
asked by Víctor Gonzal 24.01.2016 в 20:16
source

2 answers

3

Something similar happened to me, but using JavaScript, apparently Parse does not accept that you give chains like Pointers, you have to pass an object.

The solution is that when doing the query, you use an object in the% share% part. I do not know anything about Swift but, using the documentation, I suppose something like this could work:

# ...
PFObject *subcategoria = [PFObject objectWithClassName:@"subcategorias"];
subcategoria[@"id"] = @"gL8qGBhXMI";
queryHot.whereKey("come_subc_id", equalTo: subcategoria)
# ...

I hope the code is correct but the idea is similar to the answer given to me in my question How to make a query a Pointer field with the object's ID? .

    
answered by 25.01.2016 / 01:02
source
1

Many thanks Cesar Bustios for the guide.

Indeed I had to adapt it to swift and it has stayed like this:

let queryHot = PFQuery(className: "comercios") //Clase
            let hotel : String = "gL8qGBhXMI"
            let pointer = PFObject(withoutDataWithClassName:"subcategorias", objectId: hotel)
            print(pointer)
            queryHot.includeKey("come_subc_id")
            queryHot.whereKey("come_subc_id", equalTo:pointer)

It worked!

    
answered by 26.01.2016 в 23:26