Kotlin -> Json and Cascade Delete


#1

Hi all,

i am currently concerned by a specific behaviour of java realm.
i currently have a webservice which send a json list of object (which contain also objects):

{
    "Campagnes": [
        {
            "camp_id": "89",
            "nom": "TEST",
            "societe": "L",
            "date_deb_eng": "2018-09-01",
            "date_fin_eng": "2018-09-30",
            "date_deb_mep": "2018-10-01",
            "date_fin_mep": "2018-10-31",
            "pdf": "campaign_89.pdf",
            "icon": "logo_cora_256.png",
            "actif": "1",
            "gcl": [
                {
                    "code": "005",
                    "nom": "BAUD"
                },
                {
                    "code": "009",
                    "nom": "CORA"
                },
                {
                    "code": "012",
                    "nom": "MIGROS FRANCE"
                }
            ],
            "produits": [
                {
                    "id": "",
                    "gencod": "1234567891231",
                    "label": "TEST"
                },
                {
                    "id": "",
                    "gencod": "4561237894566",
                    "label": "TEST 3"
                },
                {
                    "id": "",
                    "gencod": "7894561234560",
                    "label": "TEST 2"
                }
            ]
        },..]}

To receive all these data i have 3 objects:

open class CampagneAC(
        @PrimaryKey
        open var camp_id: Int = 0,
        open var nom: String = "",
        open var societe : String="",
        open var date_deb_eng : Date = Calendar.getInstance().time,
        open var date_fin_eng : Date = Calendar.getInstance().time,
        open var date_deb_mep : Date = Calendar.getInstance().time,
        open var date_fin_mep : Date = Calendar.getInstance().time,
        open var pdf : String= "",
        open var icon : String="",
        open var gcl : RealmList<GclAC> = RealmList(),
        open var produits : RealmList<ProduitsAC> = RealmList(),
        open var actif: Int = 1
        ) : RealmObject()
open class ProduitsAC(
        open var id : String="",
        open var gencod : String="",
        open var label : String=""
): RealmObject()
open class GclAC(
        open var code : String="",
        open var nom : String=""
) : RealmObject()

On weebservice response i just call:

realm.createOrUpdateAllFromJson(CampagneAC::class.java,myJsonData)

It’s fine and works correctly on the first load but when the data is reloaded, obviously my CampagneAC values are updated, but the nested values contained in GclAC and ProductAC became orphans and still live in my realm database. How could i correct this and does the nested data could be deleted by cascade?

Thanks to everybody who takes the time to investigate my problem.