Reputation: 3908
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;
}
}
Upvotes: 1
Views: 1634
Reputation: 76
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:
Friends
object, the User class must have a List of Friend
object.Upvotes: 1