Question

j'utilise un Parse.Relation pour regrouper les utilisateurs sous un sujet.Comment récupérer tous les sujets qui ont un topic.relation pointant vers un utilisateur donné ?

La question est de savoir comment faire cela en un seul appel/rappel.

// first add() a User object to the Topic's Parse.Relation
this.friendRelation = this.topic.relation("friend");
this.friendRelation.add(user);
// save the Topic to save its newly added .relation to Parse/Mongo
this.topic.save();

// iterate by adding the same User to several Topics

// (...)

// then you want to retrieve all Parse.Relations of all Topics where that 
// specific user is pointed to

// the non-optimized way is to traverse every row in the Topics index 
// and query each topic for its relations to find matching pointers to our user, 
// which means that the number of calls is bound to the number of rows – fine for
// 10 topics in Mongo but passed 100 it won't be tolerable.
Était-ce utile?

La solution

Construisez une requête pour la classe Topic et ajoutez une contrainte equalTo.

var query = new Parse.Query(Topic);
query.equalTo("friend", user);
query.find({success: function (returnedTopics) {
    ...
    },
    error: function (...) {
    }
});

Cela renverra tous les objets Topic qui contiennent un utilisateur dans leur relation amie.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top