Coding John
Coding John

Reputation: 53

Could not map json for Realm DB

I wanna create some Objects in my Realm using "realm.createOrUpdateAllFromJson(type.class, json-array)". But it throws error -> could not map json.

Heres the Json-Array. It's valid.

[
    {
        "uuid": "99975b79-adac-4baa-a1ab-96c0db23c0ee",
        "name": "Joris"
    },
    {
        "uuid": "0",
        "name": "Adrian"
    },
    {
        "uuid": "0",
        "name": "Oliver"
    },
    {
        "uuid": "0",
        "name": "Michael"
    },
    {
        "uuid": "0",
        "name": "David"
    },
    {
        "uuid": "0",
        "name": "Daniel"
    }
]

and heres my Owner-Model.

public class Owner extends RealmObject{

@PrimaryKey
private String      uuid; //PK
private String      name;
private String      token;
private RealmList<Idea> ideas;
private RealmList<Vote> votes;

Here's the code where the error is thrown. its in the 3. line

    realm.beginTransaction();
    realm.createOrUpdateAllFromJson(Tag.class, tagJson);
    realm.createOrUpdateAllFromJson(Owner.class, ownerJson);
    realm.commitTransaction();

it's strange how the creating of Tag-Objects works fine.. please help!!

greetings john

Upvotes: 0

Views: 978

Answers (1)

Christian Melchior
Christian Melchior

Reputation: 20126

You UUID is marked as a @PrimaryKey and you have multiple "uuid"'s with the value 0. This means that you are breaking the primary key constraint of only having one element with each uuid. If you look closely in LogCat you will probably see that Could not map JSON exception mentions this as well.

Upvotes: 3

Related Questions