Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit number of calls in using Parse

I'm building my first application with a backend.

General info

The app allows users to upload a place, attach tags to it, pictures, etc.

Dbs

I'm using Realm as an offline Db & Parse.com as online Db.

Example

I'm building the database model and trying to link all many-to-many relations. An example: A place has a number of tags (short string), a tag can have a number of places.

To built this I did this with realm: 1. Make the place object 2. For each given tag, make a tag object and add the place object relation 3. Get the place object and add the tag object relation

Now I can get all the tags from one place, when I have the place object. I can get all the places that belong to a tag object.

I'm building the online version with Parse now, and realised that this approach will lead to many calls.

Possible solution

  • Don't add the tag object relation to the place relation (step 3) instead, query the tag class for relation with place object. (might be very slow?)
  • Is there a way to build the model 'offline' and push it as a whole?

As I'm new to this kind of logic, I hope the question is clear. I understand it is broad, but I think it is best to explain the total case.

like image 769
TomCB Avatar asked May 30 '15 07:05

TomCB


1 Answers

There are several ways to implement many-to-many relationships in Parse. You can use Arrays or Relations depending on the number of related objects. You can read more in Parse's documentation.

In Parse Relations, you can add multiple objects in a relation before making a call. Let me take Book and Author example in the documentation and adjust it for your case. The process is:

  1. Make the Place object
  2. Check if a tag object is exist. Create one if not.
  3. Associate the tag objects with place object in a Parse Relation

Here is the code sample:

// let’s say we have a few objects representing Place tags
ParseObject tagOne=
ParseObject tagTwo =
ParseObject tagThree =

// now we create a place object or specify the one you want to update
ParseObject place= new ParseObject("Place");

// now let’s associate the tags with the place
// remember to create a "tags" relation on Place
ParseRelation<ParseObject> relation = place.getRelation("tags");
relation.add(tagOne);
relation.add(tagTwo);
relation.add(tagThree);

// now save the book object
book.saveInBackground();

EDIT AS COMMENT BELOW

If you want to find all places with a given tag, you can do like:

ParseObject tag = ....
ParseQuery<ParseObject> query = ParseQuery.getQuery("Place");
query.whereEqualTo("tags",tag);
like image 113
Ralphilius Avatar answered Nov 06 '22 00:11

Ralphilius