Hello Faisal,
that's an interesting use-case!
A couple of notes:
1) In Realm you don't need an extra model class to keep track of many-to-many relationship, as you do in a relational database. All you need is something like this:
Movie:
* title
* RealmList<User> watchers; // you only need this field if you need queries going in the other direction as well
User:
* name
* RealmList<Movie> moviesWatched.
2) The duplication or objects happens because you are using standalone objects to insert the data. SInce primary-keys are not available yet (soon!) there's no way for Realm to know if it's actually the same user and movie you are inserting, so it adds a new one.
Also bear in mind that using standalone objects is a waste of memory.
One way to do it, given for the sake of the argument you want to keep the WatchedByUser class, is something like:
User user = realm.where(User.class)....
Movie movie = realm.where(Movie.class)....
realm.beginTransaction();
WatchedByUser wbu = realm.createObject(WatchedByUser.class);
wbu.setUser(user);
wbu.setMovie(movie);
realm.commitTransaction();
Does this help you?
--
Emanuele Zattin