Android please jump to this address: juejin.cn/post/696680…

Reason for use: Android SharedPreferences multithreaded value invalid, a bear brother recommended to use xiangbaopiaopiammkV storage sharps, not to lose money. However, I hold the mentality of losing money to play, really sweet!!

MMKV advantage

  • Use one of the zero-copy techniquesmmapMemory mapped key-value component. User space can share data from kernel space, reducing the number of copies from kernel space to user space
  • Use the best performanceprotobufagreement
  • Stable, first used on iOS, and later on Android can also use this really fragrant tool
  • It’s much tastier than NSUserDefaults

Then start playing

throughCocoaPodsThe installation

  • Podfile file, add pod ‘MMKV’
  • Header file addition#import <MMKV/MMKV.h>

Initialize the

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [MMKV initializeMMKV:nil];
    return YES;
}
Copy the code

Smells good use

// Create default instance MMKV * MMKV = [MMKV defaultMMKV]; // Create a custom instance MMKV *mmkv2 = [MMKV mmkvWithID:@"test"]; // set Boolean [MMKV setBool:YES forKey:@"bool"]; // Set BoolForKey [MMKV getBoolForKey:@"bool"]; [MMKV setInt32:-1024 forKey:@"int32"]; [MMKV getInt32ForKey:@"int32"]; // Set value 32-bit unsigned integer [MMKV setUInt32: STD ::numeric_limits<uint32_t>:: Max () forKey:@"uint32"]; // Get value specifies a 32-bit unsigned integer. [MMKV getUInt32ForKey:@"uint32"]. [MMKV setInt64: STD ::numeric_limits<int64_t>::min() forKey:@"int64"]; [MMKV getInt64ForKey:@"int64"]; // Set value 64-bit unsigned integer [MMKV setUInt64: STD ::numeric_limits<uint64_t>:: Max () forKey:@"uint64"]; // Get value 64-bit unsigned integer [MMKV getInt64ForKey:@"uint64"]; // Set value string [MMKV setString:@"hello, MMKV "forKey:@"string"]; [MMKV getStringForKey:@"string"]; // set value float [MMKV setFloat:30.0 forKey:@"float"]; // get value float [MMKV getFloatForKey:@"float"]; // set value double [MMKV setDouble: STD ::numeric_limits<double>:: Max () forKey:@"double"]; // get value double [MMKV getDoubleForKey:@"double"]; // Set value object [MMKV setObject:nil forKey:@"string"]; / / get value object [MMKV getObjectOfClass: nsstrings. Class forKey: @ "string"]. // Set value date [MMKV setDate:[NSDate date] forKey:@"date"]; // get value date [MMKV getDateForKey:@"date"]; // get allKeys [MMKV allKeys]; Mmkv.totalsize; Key [MMKV containsKey:@"string"]; [MMKV removeValueForKey:@"test"] [MMKV removeValuesForKeys:@[@"test1", @"test2"]]; // Delete the cache without deleting the disk file [MMKV clearMemoryCache]; // delete all key values, delete disk file [MMKV close];Copy the code