Question

I am using Parse and created a one-to-one relationship between two models (a location has a queue). How can I retrieve an attribute of the queue using just the location?

Was it helpful?

Solution

I just started using Parse. According to their Android documentation, you need to add the queue ParseObject to the location ParseObject before you store them (or vice versa).

Assuming you put the relationship between the two in the location object, , you should be able to pull the queue with something like this:

Storing:

// Create location
ParseObject location = new ParseObject("Location");
location.put("foo", "bar");

// Create queue
ParseObject queue = new ParseObject("Queue");
queue.put("name", "Ben");

// Store the queue in the location (location will contain a pointer to queue)
location.put("Queue", queue);

// Save both location and queue
location.saveInBackground();

Retrieving:

// Retrieve location using objectId
ParseQuery query = new ParseQuery("Location");
query.getInBackground("QkKt30WhIA", new GetCallback() { // objectId!
    public void done(ParseObject object, ParseException e) {
        if (e == null) {
            // Location found! Query for the queue
            object.getParseObject("Queue").fetchIfNeededInBackground(new GetCallback() {
                public void done(ParseObject object, ParseException e) {
                    // Queue found! Get the name
                    String queueAttr = object.getString("name");
                Log.i("TEST", "name: " + queueAttr);
                }
            });
        }
        else {
            // something went wrong
            Log.e("TEST", "Oops!");
        }
    }
});

OTHER TIPS

This is what I ended up doing:

        // create qLocation
        ParseObject qLocation = new ParseObject("QLocations");
        qLocation.put("name", qname);
        qLocation.put("streetAddress", streetAddress);
        qLocation.put("cityState", cityState);

        // create qLine               
        ParseObject qLine = new ParseObject("Lines");
        Random r = new Random();
        qLine.put("length", r.nextInt(10)); //set line length to a random number between 0-10
        qLine.put("qName",qname);

        // add relationship
        qLine.put("parent", qLocation);

        // save line and location
        qLine.saveInBackground();

In the end, line qline.put("parent", qLocation); didn't end up mattering because I had no idea how to use that relationship to get the queue from a given location. So I just ended up using the column "qName" in qLine to check which queue was associated with which location.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top