2
votes

I have seen the tutorial but its not clear to me how to handle the following problem and optimize my query. I am doing a query inside a query for each item, and of course this is not fine since I am sure it can be done with one query.

I have the following objects:

User - a User logged in using FB logging

Publication - a publication created by the user (has a field called createdBy with a pointer to the user

Parse.Installation - the Installation object to create a push, has a reference to the user.

Right now what I am doing is a query by date, looking for expired publications as follows:

var Publication = Parse.Object.extend("Publication");
var query = new Parse.Query(Publication);
//calculate the date here......
query.lessThan("createdAt", expirationDate);
query.find({
success: function(results) {

//here I have all the publications.
//Now I get the user
var aPub = results[i];
var user = aPub.get('createdBy');
var query2 = new Parse.Query(Parse.Installation);
query2.equalTo('user', user);

//and now I send the push for EACH user
Parse.Push.send({
where: query2,
data: {....
...
...

I am sure this can be done with one query but I don't understand how.

Thanks.

1

1 Answers

3
votes

For the second part of your query, this is still using several queries but only one API call.

var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo("objectId",  aPub.get('createdBy')id;

// Find devices associated with these users
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.matchesQuery('user', userQuery);

// Send push notification to query
Parse.Push.send({
    where: pushQuery,
    data: {}
}, {
    success: function() {
        // Push was successful
    },
    error: function(error) {
        // Handle error
    }
});`

in addition, you don't need to use find(). you can look into these methods:

containedIn()

matchesQuery()

matchesKeyInQuery()