Realm relationship in models

38 views
Skip to first unread message

Reza Shah

unread,
Jan 21, 2016, 8:13:47 AM1/21/16
to Realm Java
Hi,

I have some doubt regarding how i should create the models.
My app will sync some data from server. For example, i have lists of Account which joined several projects.
One project can be joined by several Accounts. At one time only one Account can active in the app.

Currently 'm using these models:

class Account extends RealmObject {
     private long id;
     private String name;
     private RealmList<Project> projects;

class Project extends RealmObject {
     private long Id;
     private String name;
}

The behaviour are as follow:
1. Account1 is active after sync:
    Account1 projects are ProjectA, ProjectB and ProjectD

2. Then activate Account2, after sync:
     Account2 projects are ProjectA, ProjectB

3. Removed Account1 from ProjectA in the server.

4. Re-activate Account1, after sync:
    Account1 projects are ProjectB and ProjectD

My sync process is simple, just delete all projects currently joined by Account1 then reinsert new data.

My question is, what would be the content of projects for Account2(before sync to server again)? 
Will Account2 lost ProjectA.

I created the models as above because i want to cache the Project data. 
I'm not sure it will work, and i guess i will lost ProjectA data for Account2 too.

Should i introduce another class? Something like:
class JoinedProject extends RealmObject {
     private long accountId;
     private Project project;
}

Then change the Account class into 
class Account extends RealmObject {
     private long id;
     private String name;
     private RealmList<JoinedProject> joinedProjects;
}


Thank you very much.

Mulong Chen

unread,
Jan 21, 2016, 8:49:47 PM1/21/16
to Reza Shah, realm...@googlegroups.com
If you only delete `accountA` without deleting any `Project` objects, all of the projects will not be affected.
Which means contents in `RealmList<Project> projects` won't be deleted when deleting the parent Account.

--
Mulong Chen

{#HS:163191749-2751#}
--
You received this message because you are subscribed to the Google Groups "Realm Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-java+...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-java/7f942de5-3a2b-4d54-b0cc-787efe448330%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Reza Shah

unread,
Jan 21, 2016, 9:07:12 PM1/21/16
to Realm Java, rsha...@kireihana.com, he...@realm.io
Thank you.

I just realized the 3rd step might be confusing.
"3. Removed Account1 from ProjectA in the server."
In the server, i removed the AccountA as the member of ProjectA. In device AccountA is not deleted.

So after sync, Account1 only joined to ProjectB and ProjectD. 
And my sync method is to delete all projects from AccountA then re-insert with the data from server again.

So before 3rd step Account1 have ProjectA, ProjectB and ProjectD. 
Then after 3rd step, i will delete ProjectA, ProjectB and ProjectD then re-insert ProjectB and ProjectD.
Reply all
Reply to author
Forward
0 new messages