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
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.
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:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With