1

I'm doing an api call, which response includes list of objects, the json response is:

[
{
    "name": "jay",
    "age": 27,
    "avatar": "https://avatars.abc/bat_man/",
    "friends": [{
            "friend_name": "abc",
            "friend_age": 23,
            "avatar": "https://avatars.abc/thor/"
        },
        {
            "friend_name": "xyz",
            "friend_age": 26,
            "avatar": "https://avatars.abc/hulk/"
        },
        {
            "friend_name": "pqr",
            "friend_age": 28,
            "avatar": "https://avatars.abc/iron_man/"
        }
    ]
},

{
    "name": "ajay",
    "age": 27,
    "avatar": "https://avatars.abc/bat_man/",
    "friends": [{
            "friend_name": "abc",
            "friend_age": 23,
            "avatar": "https://avatars.abc/thor/"
        },
        {
            "friend_name": "xyz",
            "friend_age": 26,
            "avatar": "https://avatars.abc/hulk/"
        },
        {
            "friend_name": "pqr",
            "friend_age": 28,
            "avatar": "https://avatars.abc/iron_man/"
        }
     ]
  }

]

I created a model without the friends property, as soon as I added a List of Friend I was not able to use room, Now the model I created will work for RetroFit, but it will not work for Room, since Room doesn't support List Objects inside entity.

I fixed it using @TypeConverter, but after that the retrofit stopped working. I'm looking for an approach, so that I can use the same Model for both(api call + persisting the data through room).

I tried to make the model to work with ROOM, but after that my retrofit api callback is coming to onFailure() for the obvious reason that it is not getting parsed, the error I'm receiving is Failed sending reply to debugger: Broken pipe.

Is there is any way so I ignore some particular property in Retrofit call, and use same model for both purpose.

My model looks :

@Entity(tableName = "user_table")
public class User {
    private String name;
    private String href;
    private String avatar;
    private Friends friend;

public User(String name, String href, String avatar, Friends friend) {
    this.name = name;
    this.href = href;
    this.avatar = avatar;
    this.friend = friend;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getHref() {
    return href;
}

public void setHref(String href) {
    this.href = href;
}

public String getAvatar() {
    return avatar;
}

public void setAvatar(String avatar) {
    this.avatar = avatar;
}

public Friends getFriend() {
    return friend;
}

public void setFriend(Friends friend) {
    this.friend = friend;
}
}
Soutzikevich
  • 991
  • 3
  • 13
  • 29
HAXM
  • 3,578
  • 4
  • 31
  • 38
  • Where are you saving the friends list? You should have your friends in another table and use a Foreign Key in Room. – Shalon Isaac May 20 '19 at 22:25
  • Did you check json key `friends` with the `friend` field of `Entity`? Btw instead of creating Friends object you can directly write `List` here and create typeconverter for it. – mallaudin May 20 '19 at 22:25
  • You can read more about `List Conversion` here. https://stackoverflow.com/questions/44580702/android-room-persistent-library-how-to-insert-class-that-has-a-list-object-fie – mallaudin May 20 '19 at 22:28

1 Answers1

1

You can use your first approach by annotating with @Ignore the fields you want to ignore in room

@Entity(tableName = "user_table")
public class User {
    private String name;
    private String href;
    private String avatar;
    @Ignore private Friends friend;

Assuming you have in your Dao something like this

@Dao

public interface MyDao {

    @Insert
    public void insertUser(User user);

    @Insert
    public void insertFriends(List<Friend> friends);
}

Then you can add your data as follows

myDao.insertUser(user)
myDao.insertFriends(user.getFriend())

Using this approach, there are some changes you'll have to do:

  • Instead of a Friends object, the User class must have a List of Friend object.
  • You have to create a table Friends with the appropiate Foreign Keys (this includes adding a userId field to the friends object)
  • Thanks for your answer, but this doesn't helped me, I want to ignore it for RetroFit not Room. still I tried this and the result was same, my retrofit call is failing. – HAXM May 21 '19 at 03:39
  • Ok. Have you tried to use the @Expose annotation? That's what I use when I want to ignore a field in retrofit – Shalon Isaac May 21 '19 at 09:42