EasyStorer, more elegant and convenient access to data objects.


Still using heavy SharedPreferences Android SDK is incorrect?
Or made repeated SQLite database?
Better to try this EasyStorer?

EasyStorer advantage

  • Very low cost of learning.
  • Quick access method invocation.
  • Serializable object encapsulates access.
  • Use synchronization lock to ensure thread safety.
  • A stored everywhere desirable (server (later versions), customer service side).

Use the premise EasyStorer

  • Application needs to be initialized in the onCreate (), otherwise when you call EasyStorer will throw a runtime exception , interrupting the running program.
  • For the class of non-Java SDK package and its parent class member variables and related variables, and so class members and must implement the empty interface Serializable, class package name must not begin with java .
    • Generally as long as they write the class needs to implement Serializable empty interface.
    • If you only need to access the underlying data type or types of packaged Java , you do not need to achieve.

EasyStorer use

There EasyStorer static function, how kind, looked at the class structure, probably I do not say how to use a pair of?
Here Insert Picture Description
Modify the app's file build.gradle

dependencies {
	...
	api "com.github.isong0623:EasyStorer:1.0-support"
	...
}

Application in the initialization (not the new Application class)

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        EasyStorer.init(this);//在这里初始化
    }
}

If you need to specify the new App Application in the AndroidManifest.xml

    <application
    	...
    	android:name=".App"
    	...>

official use

Create a custom class

class Obj implements Serializable{//只需要实现这个接口
    int a = 100;
    int b = 1000;
    //ObjA o; 如果成员变量为非Java定义类型 则它自己及它的成员也必须实现Serializable接口
    Obj(){}
    Obj(int a, int b){
        this.a = a;
        this.b = b;
    }

    @Override
    public String toString() {
        return String.format("val1:%d val2:%d\n",a,b);
    }
}
Simple Storage Example
		EasyStorer.clear();//清除默认库里的所有数据

        EasyStorer.put("Obj", new Obj(123,132));
        EasyStorer.put("String","String");
        EasyStorer.put("byte",(byte)10);
        EasyStorer.put("Byte",new Byte((byte) 15));
        EasyStorer.put("short",(short)10);
        EasyStorer.put("Short",new Short((short)15));
        EasyStorer.put("boolean",true);
        EasyStorer.put("Boolean",new Boolean(true));
        EasyStorer.put("int",10);
        EasyStorer.put("Integer",15);
        EasyStorer.put("long",10L);
        EasyStorer.put("Long",new Long(15L));
        EasyStorer.put("float",10f);
        EasyStorer.put("Float",15f);
        EasyStorer.put("double",10d);
        EasyStorer.put("Double",new Double(15d));
        EasyStorer.put("List",new ArrayList<>(Arrays.asList(new String[]{"123","456"})));
        EasyStorer.put("Set",new HashSet<>(Arrays.asList(new String[]{"123","456"})));
        EasyStorer.put("Map",new HashMap<>(new HashMap<String,String>(){{put("abc","edf");}}));
Simple for an example
 		Log.e("TestEasyStorer",EasyStorer.get("Obj",new Obj())+"");
        Log.e("TestEasyStorer",EasyStorer.get("String","null"));
        Log.e("TestEasyStorer",EasyStorer.get("byte",(byte)0)+"");
        Log.e("TestEasyStorer",EasyStorer.get("Byte",new Byte((byte)0))+"");
        Log.e("TestEasyStorer",EasyStorer.get("short",(short)0)+"");
        Log.e("TestEasyStorer",EasyStorer.get("Short",new Short((short)0))+"");
        Log.e("TestEasyStorer",EasyStorer.get("boolean",false)+"");
        Log.e("TestEasyStorer",EasyStorer.get("Boolean",new Boolean(false))+"");
        Log.e("TestEasyStorer",EasyStorer.get("int",0)+"");
        Log.e("TestEasyStorer",EasyStorer.get("Integer",new Integer(0))+"");
        Log.e("TestEasyStorer",EasyStorer.get("long",0L)+"");
        Log.e("TestEasyStorer",EasyStorer.get("Long",new Long(0L))+"");
        Log.e("TestEasyStorer",EasyStorer.get("float",0f)+"");
        Log.e("TestEasyStorer",EasyStorer.get("Float",new Float(0f))+"");
        Log.e("TestEasyStorer",EasyStorer.get("double",0d)+"");
        Log.e("TestEasyStorer",EasyStorer.get("Double",new Double(0d))+"");
        Log.e("TestEasyStorer",EasyStorer.get("List",new ArrayList<String>()).toArray()[0]+"");
        Log.e("TestEasyStorer",EasyStorer.get("Set",new HashSet<>()).toArray()[0]+"");
        Log.e("TestEasyStorer",EasyStorer.get("Map",new HashMap<>()).keySet().toArray()[0]+"");
Sample output
E/TestEasyStorer: val1:123 val2:132
E/TestEasyStorer: String
E/TestEasyStorer: 10
E/TestEasyStorer: 15
E/TestEasyStorer: 10
E/TestEasyStorer: 15
E/TestEasyStorer: true
E/TestEasyStorer: true
E/TestEasyStorer: 10
E/TestEasyStorer: 15
E/TestEasyStorer: 10
E/TestEasyStorer: 15
E/TestEasyStorer: 10.0
E/TestEasyStorer: 15.0
E/TestEasyStorer: 10.0
E/TestEasyStorer: 15.0
E/TestEasyStorer: 123
E/TestEasyStorer: 123
E/TestEasyStorer: abc

doubt

Q: Why are there two and put two GET?
A: All of our data references are on the SQLite database, a function of two parameters specifies the default database (/data/data/com.example.package/databases / Easy_Storer .db_), at the same time, there is a named folder to the database (/data/data/com.example.package/databases/ Easy_Storer /) for storing data serialization, to name. We can use a different database name operate on different groups, for example, we use "user_info" store user information with "app_info" storage applications such as the role of information applications easier to manage.

Detailed API

initialization
	//初始化context,在这里仅用来获取内部数据库存储路径
	public static void init(Context context);
Data fetch
	/**
     * 取数据操作
     * @param tag 标签
     * @param defaultValue 取不到时返回的值
     * @param <T>
     * @return 在默认库(_Easy_Storer_)中取出标签为tag且类型为T的对象
     */
	public static <T> T get(String tag,T defaultValue);

 	/**
     * 取数据操作
     * @param tag 标签
     * @param defaultValue 取不到时返回的值
     * @param databaseName 要操作的数据库名称
     * @param <T>
     * @return 在指定库(databaseName )中取出标签为tag且类型为T的对象
     */
    public static <T> T get(String tag,T defaultValue,String databaseName);
Data storage operation
	/**
     * 在默认库(_Easy_Storer_)中存放对象obj置标签为tag
     * @param tag 存放标签
     * @param obj 待存放的对象
     * @return 返回是否存放成功
     */
    public static boolean put(String tag,Object obj);

 	/**
     * 在指定库(databaseName)中存放对象obj置标签为tag
     * @param tag 存放标签
     * @param obj 待存放的对象
     * @param databaseName 要存放数据的数据库名
     * @return 返回是否存放成功
     */
    public static boolean put(String tag,Object obj,String databaseName);
Delete data manipulation
	/**
     * 删除默认库(_Easy_Storer_)标签为tag的所有对象数据
     * @param tag 要删除的标签
     * @return 删除是否成功
     */
    public static boolean remove(String tag)

	/**
     * 删除指定数据库(databaseName)中标签为tag的所有数据
     * @param tag
     * @param databaseName
     * @return 删除是否成功
     */
    public static boolean remove(String tag,String databaseName)

	/**
     * 删除默认库(_Easy_Storer_)中标签为tag,类型为clazz的数据
     * @param tag
     * @param clazz
     * @return 删除是否成功
     */
    public static boolean remove(String tag,Class clazz)

	/**
     * 删除指定库(databaseName)中标签为tag,类型为clazz的数据
     * @param tag
     * @param clazz
     * @param databaseName
     * @return
     */
    public static boolean remove(String tag,Class clazz, String databaseName)
Empty data manipulation
	//删除默认数据库(_Easy_Storer_)的所有对象数据
	public static boolean clear();

	//删除指定数据库(databaseName)中的所有对象数据
	public static boolean clear(String databaseName)
Published 111 original articles · won praise 32 · views 40000 +

Guess you like

Origin blog.csdn.net/best335/article/details/104772571