lowlevel-studios / storo

An Android library to cache any serializable objects to disk, using a LRU cache implementation, with the possibility to specify an expiry time for each entry and a maximum size that can be allocated.
88 stars 13 forks source link

when i store object that has array, it doesnt store it #4

Open hishambakr opened 7 years ago

hishambakr commented 7 years ago

thanks for great support. please give me an example of storing array of objects as i failed to do that

waninkoko commented 7 years ago

Hi,

There aren't any special requirements to store and retrieve an object array. An example could be the following:

Item[] array = new Item[] { ... };
Storo.put("items", array).execute();

Item[] arrayFromCache = Storo.get("items", Item[].class).execute();

If you are still having troubles, can you post how are you storing the array?

hishambakr commented 7 years ago

sorry i meant list. i tried storing list directly, it gave me compilation error. i wrapped list inside an object which resolve compilation error. but when i get object, it return null. i will try array. i will post example when i return home

iRYO400 commented 7 years ago

@hisham2007 how is it going?

waninkoko commented 7 years ago

I was waiting for the example to provide a precise example code but I think I'll just post this generic example:

ArrayList<Item> list = ...;
Storo.put("items", list).execute();

Type type = new TypeToken<ArrayList<Item>>(){}.getType();
Get<ArrayList<Item>> get = Storo.get("items", type);

ArrayList<Item> listFromCache = get.execute();
hishambakr commented 7 years ago

sorry i was focusing on critical issues in my app. i will try and let you know. forgive me if i take long time.

iRYO400 commented 7 years ago

@waninkoko thanks for providing code, it opened a new things for me. as I understood, Storo doesn't store list, if it implements any interface, right? I get it, when I store list of models that implement interface ParentListItem( library link https://github.com/bignerdranch/expandable-recycler-view)

iRYO400 commented 7 years ago

anybody here?

waninkoko commented 7 years ago

@iRYO400 It should not matter if the class implements an interface. Just be sure that the class has an empty constructor and the attributes are declared public.

iRYO400 commented 7 years ago

@waninkoko anyway, it doesn't work. Mby because I have difficult structure, List1 with List2, and each object of List2 has List3, List4, List5

waninkoko commented 7 years ago

@iRYO400 I have an app where I'm storing an object that contains multiple lists and it is working without any issues. It should be working with your structure too.

Can you post the class or something similar to your structure?

iRYO400 commented 7 years ago

@waninkoko First class named as GroupModel:

public class MainMatchGroupModel implements ParentListItem {

    public long turnirID;
    public String turnirName;
    public String turnirCountry;
    public int priority;
    public List<MainMatchChildModel> list;

    public MainMatchGroupModel() {
    }

...
getters + setters
...

    @Override
    public List<?> getChildItemList() {
        return list;
    }

    @Override
    public boolean isInitiallyExpanded() {
        return false;
    }
}

Second class - ChildModel:

public class MainMatchChildModel {

    long id;
    String dTime;
    String matchStatus;
    String matchScore;
    int matchMinute;
    int matchPart;
    String place;
    String judges;
    long homeId;
    String homeName;
    String homeLogo;
    String homeCountry;
    int homeRedCard;
    int homeYellowCard;
    int homeCorners;

    //guest
    long guestId;
    String guestName;
    String guestLogo;
    String guestCountry;
    int guestRedCard;
    int guestYellowCard;
    int guestCorners;

    long questionID;
    long turnirID;
    String turnirName;
    String turnirCountry;

    List<OddOfMatch> oddOfMatches;
    List<OpisanieObzorHistory> matchTextHistory;
    List<LentaNewsModel> feedsList;

...
getters + setters
...

To store it, I call next after parsing JSON. "listTurnirs" is a list of previous class

Storo.put("listTurnirs", listTurnirs).async(new Callback<Boolean>() {
                            @Override
                            public void onResult(Boolean aBoolean) {
                                if (aBoolean) {
                                    Log.d("TAG", "Success result");
                                } else {
                                    Log.d("TAG", "Failed storing");
                                }
                            }
                        });

in LogCat I always get "Success result"

To retreive list use next

        Type type = new TypeToken<ArrayList<MainMatchGroupModel>>(){}.getType();
        Get<ArrayList<MainMatchGroupModel>> get = Storo.get("listTurnirs", type);
        listTurnirs = get.execute();

in this step, it always return null But if I use ChildModel It works without any exception

waninkoko commented 7 years ago

@iRYO400 Can you try the following changes?

First, when initializing Storo, add a custom Gson instance like this:

        StoroBuilder.configure(5000)
                .setGsonInstance(new GsonBuilder()
                        .excludeFieldsWithoutExposeAnnotation()
                        .create())
                .initialize();

Then, in MainMatchGroupModel and MainMatchChildModel, add the @Expose annotation to every attribute you want to store.

iRYO400 commented 7 years ago

@waninkoko is this ok, to configure Storo in Application class? Currently I have next

  StoroBuilder.configure(8192)  // maximum size to allocate in bytes
                .setDefaultCacheDirectory(this)
                .initialize();
waninkoko commented 7 years ago

@iRYO400 Not just ok, but it is the preferred place to configure it.

Add the setGsonInstance between initialize() and configure(8192), and the@Expose annotation, and let's hope it fixes your problem.

ghost commented 7 years ago

This library has the best (and simple most!) design out of all the cache libraries listed in android-arsenal.com. Yet, my first attempt at using it is with an array. It does not return it after the app exists. It looks as if the library has some kind of a serious issue. Code below:

  1. In the App (verified and it's called without any issue): StoroBuilder.configure( 8192 ).setDefaultCacheDirectory( this ).initialize();

  2. Another attempt that yield no results: StoroBuilder.configure( 8192 ).setGsonInstance( new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() ).initialize();

  3. What is the "max number of bytes to alloc" parameter. Is it in memory? in File? Shouldn't that be dynamic? What about the range of values that can be used here? I thought that if it means the bytes "on disk" than I need some 128KB at least. Does it mean I should initialize with: StoroBuilder.configure( 131072 ).... ?

  4. I was trying to read the cache with the following line: Playlist cached = (Playlist)Storo.get( "PL_" + id, type ).execute(); After writing that array of objects with: Storo.put( "PL_" + playlistId, playlist ).execute(); I get null in cached variable.

What am I doing wrong? Would be great to have a working example! With Containers!

Thanks

huzaifa0024 commented 6 years ago

I am saving chapterObjects arraylist and it save perfectly. But retrieval is always null no matter what format i try. I used all the above mentioned methods. Kindly guide