android_tutorial

guoling edited this page Sep 18, 2018 · 1 revision

MMKV for Android

MMKV is an efficient, samll, easy-to-use mobile key-value storage framework used in the WeChat application. It's currently available on both iOS and Android.

Tutorial

You can use MMKV as you go. All changes are saved immediately, no sync, no apply calls needed.

Configuration

  • Setup MMKV on App startup, say your MainActivity, add these code:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        String rootDir = MMKV.initialize(this);
        System.out.println("mmkv root: " + rootDir);
    }

CRUD Operations

  • MMKV has a global instance, you can use it directly:

    import com.tencent.mmkv.MMKV;
    ...
    MMKV kv = MMKV.defaultMMKV();
    
    kv.encode("bool", true);
    System.out.println("bool: " + kv.decodeBool("bool"));
    
    kv.encode("int", Integer.MIN_VALUE);
    System.out.println("int: " + kv.decodeInt("int"));
    
    kv.encode("long", Long.MAX_VALUE);
    System.out.println("long: " + kv.decodeLong("long"));
    
    kv.encode("float", -3.14f);
    System.out.println("float: " + kv.decodeFloat("float"));
    
    kv.encode("double", Double.MIN_VALUE);
    System.out.println("double: " + kv.decodeDouble("double"));
    
    kv.encode("string", "Hello from mmkv");
    System.out.println("string: " + kv.decodeString("string"));
    
    byte[] bytes = {'m', 'm', 'k', 'v'};
    kv.encode("bytes", bytes);
    System.out.println("bytes: " + new String(kv.decodeBytes("bytes")));

    As you can see, MMKV is quite easy to use.

  • Deleting & Querying:

    MMKV kv = MMKV.defaultMMKV();
    
    kv.removeValueForKey("bool");
    System.out.println("bool: " + kv.decodeBool("bool"));
        
    kv.removeValuesForKeys(new String[]{"int", "long"});
    System.out.println("allKeys: " + Arrays.toString(kv.allKeys()));
    
    boolean hasBool = kv.containsKey("bool");
  • If different modules/logics need isolated storage, you can also create your own MMKV instance separately:

    MMKV* mmkv = MMKV.mmkvWithID("MyID");
    mmkv.encode("bool", true);
  • If multi-process accessing is needed,you can set MMKV.MULTI_PROCESS_MODE on MMKV initialization:

    MMKV* mmkv = MMKV.mmkvWithID("InterProcessKV", MMKV.MULTI_PROCESS_MODE);
    mmkv.encode("bool", true);

Supported Types

  • Primitive Types:

    • boolean、int、long、float、double、byte[]
  • Classes & Collections:

    • String、Set<String>

Migrating from SharedPreferences

  • MMKV provides importFromSharedPreferences(),you can migrate from SharedPreferences with one line of code;
  • MMKV implements SharedPreferences & SharedPreferences.Editor. You can just change initialization & declaration to MMKV,and leave those CRUD codes untouch.
    private void testImportSharedPreferences() {
//      SharedPreferences preferences = getSharedPreferences("myData", MODE_PRIVATE);
        MMKV preferences = MMKV.mmkvWithID("myData");
        // migration
        {
            SharedPreferences old_man = getSharedPreferences("myData", MODE_PRIVATE);
            preferences.importFromSharedPreferences(old_man);
            old_man.edit().clear().commit();
        }
        // just use it as before
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("bool", true);
        editor.putInt("int", Integer.MIN_VALUE);
        editor.putLong("long", Long.MAX_VALUE);
        editor.putFloat("float", -3.14f);
        editor.putString("string", "hello, imported");
        HashSet<String> set = new HashSet<String>();
        set.add("W"); set.add("e"); set.add("C"); set.add("h"); set.add("a"); set.add("t");
        editor.putStringSet("string-set", set);
        // commit() is not needed any more
        //editor.commit();
	}

What's Next

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.