1

I have used ignition in my app to cache my composite object,let say mStudentObject. I have cached my data successfully, the issue is , when i retrieve my object after killing app from recently running apps button(from currently running tasks button) ,i haven't fount any data against key(cached clear automatically).When i re-launch app (with out killing app from recent tasks) object retrieved properly. i don't know what is wrong with code.I want to cache my object permanently for 2 days. when ever i launch my app,app should get data from cached object either i kill app from currently running tasks or not. Any idea,please share.Here is my complete code:

public class MainActivity extends Activity {

    Button[] buttons = null;

    // ObjectLRUCache objectLRUCache = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttons = new Button[2];
        buttons[0] = (Button) findViewById(R.id.button1);// to save data
        buttons[1] = (Button) findViewById(R.id.button2); // to get data
        // final Student s = new Student("imran", 23, 16);
        buttons[0].setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (IgnetionHelper.getInstance()!= null) {
                    Log.d("test", "key contains, updating");
                    Student s = new Student("imran", 23, 16);

                    IgnetionHelper.getInstance().putData(s);
                } else{
                Log.d(""test),"instance is null..");

            }
        });
        buttons[1].setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                IgnetionHelper ddd = IgnetionHelper.getInstance();
                if (IgnetionHelper.getInstance().getData()!= null) {
                    Student s = (Student)  IgnetionHelper.getInstance().getData();
                    Log.d("test", "key contains, age is: " + s.age);

                } else {
                    Log.d("test", "data is null...");
                }

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

and My Person class is as:

public abstract class Person extends CachedModel implements Serializable{
    public String name = "";
    public int age = 0;

    public Person(){};

    public Person (String name,int age) {
        this.name=name;
        this.age=age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }



}

Student class is as:

public class Student extends Person{
    public String name = "";
    public int age = 0;


    public int rollNo = 0;


    public Student(){

    }

    public Student(String name, int age, int rollno) {

        this.rollNo = rollno;
        this.name=name;
        this.age=age;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public int getRollNo() {
        return rollNo;
    }

    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }

    @Override
    public boolean reloadFromCachedModel(ModelCache modelCache,
            CachedModel cachedModel) {

        Student student = (Student) cachedModel; 
        name =  student.name;
        age = student.age; 
        rollNo = student.rollNo; 

        return false;
    }

    @Override
    public String createKey(String id) {
        // TODO Auto-generated method stub
        return id;
    }
}

And finally, ignition helper class is as:

 public class IgnetionHelper {
        private static final String KEY_FOR_MYOBJECT = "MY_TEST_KEY";
        private static ModelCache cache;
        private final static int initialCapacity = 1000;
        private final static int maxConcurrentThreads = 3;
        private final static long expirationInMinutes = 60 * 24 * 2;
        private static IgnetionHelper mIgnetionHelper = null;

        public static IgnetionHelper getInstance() {
            if (cache == null)
                cache = new ModelCache(initialCapacity, expirationInMinutes,
                        maxConcurrentThreads);
            if (mIgnetionHelper == null)
                mIgnetionHelper = new IgnetionHelper();
            return mIgnetionHelper;
        }

        public boolean putData(CachedModel model) {

            model.setId(KEY_FOR_MYOBJECT);
            if (model.save(cache)) {
                Log.d("IgnetionHelper", "saved.....");
                return true;
            } else {
                Log.d("IgnetionHelper", "saved.....");
                return false;
            }
            // CachedModel model = Feed.find(cache, key, Feed.class);
            // if (model != null) {
            // Log.d("test", "key contains, updating");
            // Feed s = (Feed) model;
            // return s.save(cache);
            // }
        }

        public CachedModel getData() {
            return Student.find(cache, KEY_FOR_MYOBJECT, Student.class);

        }
    }
Mohammad Imran
  • 3,264
  • 4
  • 23
  • 37

2 Answers2

3

i have found solution of my problem. Let say you have student object

Student s = new Student("imran",16,23);

and then implement these methods in your ignetionhelper calss:

public static boolean putData(Object object, Context context,String key) {
    return GenericStore.saveObject(GenericStore.TYPE_MEMDISKCACHE,key, (Serializable) object, context.getApplicationContext());
}

public static Object getData(Context context,String key) {
    return GenericStore.getObject(GenericStore.TYPE_MEMDISKCACHE,key, context.getApplicationContext());
}

https://github.com/wareninja/generic-store-for-android import library given hare and then use above methods as:

IgnetionHelper.putData(s, context, IgnetionHelper.YOUR_KAY);
Student s=(Student)IgnetionHelper.getData(context,IgnetionHelper.YOUR_KAY);
SerCe
  • 5,826
  • 2
  • 32
  • 53
Mohammad Imran
  • 3,264
  • 4
  • 23
  • 37
1

You will need to use a cache that is stored to disk, when the app is killed the cache you are using appears to be cleared.

There is some information from Google about cache storage here http://developer.android.com/guide/topics/data/data-storage.html and check out this existing SO answer on some options for data caching within your app Best Way to Cache Data in Android

Community
  • 1
  • 1
Rod Burns
  • 2,104
  • 13
  • 24