-1

My json response is:

{
    "categories": [
        {
            "categoryName": "Events",
            "entityId": "QwN9goUaw6",
             .....
        }
    ],
    "entityId": "T1nnwwpjOM"
}

My initial plan was to follow how Google did it in their GithubSampleBrowser, except they change from a List Integer to a List String using StringUtil methods, so I decided to change tactic and try and follow what some others have said in here to convert it to a String and back to an ArrayList.

I thought I had the right query that I wanted to eventually do i.e.

@Query("SELECT * FROM category_table WHERE entityId in (:entityId) ORDER BY sortIndex ASC")
public abstract LiveData<List<Category>> loadById(List<String> entityId);

My models are currently looking like:

CategorySearchResult

@NonNull
@PrimaryKey
private String entityId;
@Nullable
private String query;
public List<Category> categories;
private String lastUpdated;

Category

@NonNull
@PrimaryKey(autoGenerate = true)
private int id;
private Integer sortIndex;
private String storyboardId;
@NonNull
private String entityId;
private String categoryName;
private String menuId;

My attempt at the convertors:

@TypeConverter
public static List<Category> fromStringToCategory(String value) {
    if (value != null) {
        Gson gson = new Gson();
        Type type = new TypeToken<List<Category>>() {
        }.getType();
        List<Category> categoryList = gson.fromJson(value,type);
        return categoryList;
    }
    return null;
}

@TypeConverter
public static String fromCategoryToString(List<Category> categories) {
    if (categories != null) {
        Gson gson = new Gson();
        Type type = new TypeToken<List<Category>>() {
        }.getType();
        String json = gson.toJson(categories, type);
        return json;
    }
    return null;
}

What i thought might be easier is to generate a list of entityIds of category part of categorySearchResult and build a @Relationship between them. I've looked at the android documentation on the user and pets but can't properly relate it to my data so any advice would be greatly appreciated.

Shade
  • 45
  • 2
  • 13

1 Answers1

0

In the end I kept them seperate and by putting the query as the primary key, it also seems to accept a blank query which i wasn't sure about, it would allow it to overwrite. Then I simply did the reverse for delete using the in function, probably could do with the relationship being built for cascade delete but for now, it seems to work reasonably well.

So in summary

Whenever my mutablelivedata changes, it uses that query to pass through to my CategorySearchResult which brings back the entityIds based on that as a list. I then put the categories seperately in its own class as normal.

Shade
  • 45
  • 2
  • 13