From 8933747fb8a38932b52b93eed0bed36fb8eb0ff0 Mon Sep 17 00:00:00 2001 From: guoling Date: Mon, 3 Dec 2018 21:29:56 +0800 Subject: [PATCH 01/16] fix SIGFPE --- Android/MMKV/mmkv/src/main/cpp/MMKV.cpp | 2 +- iOS/MMKV/MMKV/MMKV.mm | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Android/MMKV/mmkv/src/main/cpp/MMKV.cpp b/Android/MMKV/mmkv/src/main/cpp/MMKV.cpp index b0219e44..f1af2904 100644 --- a/Android/MMKV/mmkv/src/main/cpp/MMKV.cpp +++ b/Android/MMKV/mmkv/src/main/cpp/MMKV.cpp @@ -660,7 +660,7 @@ bool MMKV::ensureMemorySize(size_t newSize) { return false; } } else { - size_t avgItemSize = lenNeeded / m_dic.size(); + size_t avgItemSize = lenNeeded / std::max(1, m_dic.size()); size_t futureUsage = avgItemSize * std::max(8, (m_dic.size() + 1) / 2); // 1. no space for a full rewrite, double it // 2. or space is not large enough for future usage, double it to avoid frequently full rewrite diff --git a/iOS/MMKV/MMKV/MMKV.mm b/iOS/MMKV/MMKV/MMKV.mm index 06fb16d5..3beadd03 100644 --- a/iOS/MMKV/MMKV/MMKV.mm +++ b/iOS/MMKV/MMKV/MMKV.mm @@ -524,7 +524,7 @@ - (BOOL)ensureMemorySize:(size_t)newSize { static const int offset = pbFixed32Size(0); NSData *data = [MiniPBCoder encodeDataWithObject:m_dic]; size_t lenNeeded = data.length + offset + newSize; - size_t avgItemSize = lenNeeded / m_dic.count; + size_t avgItemSize = lenNeeded / std::max(1, m_dic.count); size_t futureUsage = avgItemSize * std::max(8, m_dic.count / 2); // 1. no space for a full rewrite, double it // 2. or space is not large enough for future usage, double it to avoid frequently full rewrite From fd49e98f90e875f5a9a4407d26e6ab425f9b92a4 Mon Sep 17 00:00:00 2001 From: guoling Date: Wed, 5 Dec 2018 10:22:58 +0800 Subject: [PATCH 02/16] update readme with license url --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index a1f4bc66..5883e358 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -[![license](https://img.shields.io/badge/license-BSD_3-brightgreen.svg?style=flat)](https://github.com/Tencent/MMKV/blob/master/LICENSE.txt) +[![license](https://img.shields.io/badge/license-BSD_3-brightgreen.svg?style=flat)](https://github.com/Tencent/MMKV/blob/master/LICENSE.TXT) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/MMKV/pulls) [![Release Version](https://img.shields.io/badge/release-1.0.14-brightgreen.svg)](https://github.com/Tencent/MMKV/releases) [![Platform](https://img.shields.io/badge/Platform-%20iOS%20%7C%20Android-brightgreen.svg)](https://github.com/Tencent/MMKV/wiki/home) From a7f4d8c69b76dfe9332c1fe983534a51fa668a1c Mon Sep 17 00:00:00 2001 From: guoling Date: Wed, 5 Dec 2018 20:14:18 +0800 Subject: [PATCH 03/16] README --- MMKV.podspec | 2 +- readme.md => README.md | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename readme.md => README.md (100%) diff --git a/MMKV.podspec b/MMKV.podspec index bd69b158..af2ef54c 100644 --- a/MMKV.podspec +++ b/MMKV.podspec @@ -11,7 +11,7 @@ Pod::Spec.new do |s| DESC s.homepage = "https://github.com/Tencent/MMKV" - s.license = { :type => "BSD 3-Clause", :file => "LICENSE.txt"} + s.license = { :type => "BSD 3-Clause", :file => "LICENSE.TXT"} s.author = { "guoling" => "guoling@tencent.com" } s.ios.deployment_target = "8.0" diff --git a/readme.md b/README.md similarity index 100% rename from readme.md rename to README.md From 5c2efcedc81adb3cc809af23e2fe9793b45d900e Mon Sep 17 00:00:00 2001 From: Guo Ling Date: Thu, 6 Dec 2018 21:57:38 +0800 Subject: [PATCH 04/16] Create CODE_OF_CONDUCT.md --- CODE_OF_CONDUCT.md | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 CODE_OF_CONDUCT.md diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 00000000..060b20b1 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,76 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, sex characteristics, gender identity and expression, +level of experience, education, socio-economic status, nationality, personal +appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or + advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at guoling@tencent.com. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see +https://www.contributor-covenant.org/faq From e82a28b46cd7a1b0227965412afd606fd066ebd0 Mon Sep 17 00:00:00 2001 From: guoling Date: Thu, 6 Dec 2018 22:09:11 +0800 Subject: [PATCH 05/16] update code of conduct --- README.md | 2 ++ readme_cn.md | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5883e358..9da33dd1 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,8 @@ Check out the [CHANGELOG.md](./CHANGELOG.md) for details of change history. ## Contributing If you are interested in contributing, check out the [CONTRIBUTING.md](https://github.com/Tencent/MMKV/blob/master/CONTRIBUTING.md), also join our [Tencent OpenSource Plan](https://opensource.tencent.com/contribution). + +To give clarity of what is expected of our members, MMKV has adopted the code of conduct defined by the Contributor Covenant, which is widely used. And we think it articulates our values well. For more, check out the [Code of Conduct](https://github.com/Tencent/MMKV/blob/master/CODE_OF_CONDUCT.md). ## FAQ & Feedback Check out the [FAQ](https://github.com/Tencent/MMKV/wiki/FAQ) first. Should there be any questions, don't hesitate to create [issues](https://github.com/Tencent/MMKV/issues). diff --git a/readme_cn.md b/readme_cn.md index 0b43a3dd..9512227a 100644 --- a/readme_cn.md +++ b/readme_cn.md @@ -110,6 +110,7 @@ MMKV 以 BSD 3-Clause 证书开源,详情参见 [LICENSE.TXT](https://github.c ## 版本历史 具体版本历史请参看 [CHANGELOG.md](./CHANGELOG.md)。 -## 参与贡献 如果你有兴趣参与贡献,可以参考 [CONTRIBUTING.md](https://github.com/Tencent/MMKV/blob/master/CONTRIBUTING.md)。 [腾讯开源激励计划](https://opensource.tencent.com/contribution) 鼓励开发者的参与和贡献,期待你的加入。 +## 参与贡献 如果你有兴趣参与贡献,可以参考 [CONTRIBUTING.md](https://github.com/Tencent/MMKV/blob/master/CONTRIBUTING.md)。 [腾讯开源激励计划](https://opensource.tencent.com/contribution) 鼓励开发者的参与和贡献,期待你的加入。 为了明确我们对参与者的期望,MMKV 采用了被广泛使用的、由 Contributor Covenant 所定义的行为准则。我们认为它很好地阐明了我们的价值观。有关更多信息请查看 [Code of Conduct](https://github.com/Tencent/MMKV/blob/master/CODE_OF_CONDUCT.md)。 + ## 问题 & 反馈 常见问题参见 [FAQ](https://github.com/Tencent/MMKV/wiki/FAQ_cn),欢迎提 [issues](https://github.com/Tencent/MMKV/issues) 提问反馈。 From b872198f127f0cbac9d07d28bdc0311845bd408c Mon Sep 17 00:00:00 2001 From: guoling Date: Mon, 10 Dec 2018 11:36:42 +0800 Subject: [PATCH 06/16] fix multi thread callback crash --- .../MMKV/mmkv/src/main/cpp/native-bridge.cpp | 2 +- Android/MMKV/mmkvdemo/build.gradle | 4 ++-- .../com/tencent/mmkvdemo/MainActivity.java | 23 +++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp b/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp index aedc3709..43db597f 100644 --- a/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp +++ b/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp @@ -32,7 +32,7 @@ static jclass g_cls = nullptr; static jfieldID g_fileID = nullptr; static jmethodID g_callbackOnCRCFailID = nullptr; static jmethodID g_callbackOnFileLengthErrorID = nullptr; -static JNIEnv *g_currentEnv = nullptr; +thread_local static JNIEnv *g_currentEnv = nullptr; extern "C" JNIEXPORT JNICALL jint JNI_OnLoad(JavaVM *vm, void *reserved) { JNIEnv *env; diff --git a/Android/MMKV/mmkvdemo/build.gradle b/Android/MMKV/mmkvdemo/build.gradle index 371d22ce..c55f2718 100644 --- a/Android/MMKV/mmkvdemo/build.gradle +++ b/Android/MMKV/mmkvdemo/build.gradle @@ -62,8 +62,8 @@ repositories { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') -// implementation project(':mmkv') - implementation 'com.tencent:mmkv:1.0.14' + implementation project(':mmkv') +// implementation 'com.tencent:mmkv:1.0.14' // implementation 'com.tencent:mmkv-static:1.0.14' implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' diff --git a/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java b/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java index 658ac643..c25db381 100644 --- a/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java +++ b/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java @@ -330,6 +330,29 @@ private void testInterProcessReKey() { prepareInterProcessAshmemByContentProvider(KEY_2); } + private void testHolderForMultiThread() { + final int COUNT = 1; + final int THREAD_COUNT = 1; + final String ID = "Hotel"; + final String KEY = "California"; + final String VALUE = "You can checkout any time you like, but you can never leave."; + + final MMKV mmkv = MMKV.mmkvWithID(ID); + Runnable task = new Runnable() { + public void run() { + for (int i = 0; i < COUNT; ++i) { + mmkv.putString(KEY, VALUE); + mmkv.getString(KEY, null); + mmkv.remove(KEY); + } + } + }; + + for (int i = 0; i < THREAD_COUNT; ++i) { + new Thread(task, "MMKV-" + i).start(); + } + } + @Override public MMKVRecoverStrategic onMMKVCRCCheckFail(String mmapID) { return MMKVRecoverStrategic.OnErrorRecover; From 42c1023fe5a013a750e8235f2788098d3421f7fd Mon Sep 17 00:00:00 2001 From: TL Date: Mon, 10 Dec 2018 14:55:04 +0800 Subject: [PATCH 07/16] add set/get function --- .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++++++++ iOS/MMKV/MMKV/MMKV.h | 5 +++++ iOS/MMKV/MMKV/MMKV.mm | 14 ++++++++++++++ iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift | 5 ++++- iOS/MMKVDemo/MMKVDemo/ViewController.mm | 5 ++++- iOS/MMKVDemo/MMKVMacDemo/ViewController.mm | 5 ++++- 6 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 iOS/MMKV/MMKV.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/iOS/MMKV/MMKV.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/iOS/MMKV/MMKV.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 00000000..18d98100 --- /dev/null +++ b/iOS/MMKV/MMKV.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/iOS/MMKV/MMKV/MMKV.h b/iOS/MMKV/MMKV/MMKV.h index 6b07c6fb..ab47644a 100644 --- a/iOS/MMKV/MMKV/MMKV.h +++ b/iOS/MMKV/MMKV/MMKV.h @@ -56,6 +56,8 @@ NS_ASSUME_NONNULL_BEGIN - (BOOL)setDouble:(double)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:)); +- (BOOL)setString:(NSString *)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:)); + - (nullable id)getObjectOfClass:(Class)cls forKey:(NSString *)key NS_SWIFT_NAME(object(of:forKey:)); - (BOOL)getBoolForKey:(NSString *)key __attribute__((swift_name("bool(forKey:)"))); @@ -79,6 +81,9 @@ NS_ASSUME_NONNULL_BEGIN - (double)getDoubleForKey:(NSString *)key NS_SWIFT_NAME(double(forKey:)); - (double)getDoubleForKey:(NSString *)key defaultValue:(double)defaultValue NS_SWIFT_NAME(double(forKey:defaultValue:)); +- (nullable NSString *)getStringForKey:(NSString *)key NS_SWIFT_NAME(string(forKey:)); +- (nullable NSString *)getStringForKey:(NSString *)key defaultValue:(nullable id)defaultValue NS_SWIFT_NAME(string(forKey:defaultValue:)); + - (BOOL)containsKey:(NSString *)key NS_SWIFT_NAME(contains(key:)); - (size_t)count; diff --git a/iOS/MMKV/MMKV/MMKV.mm b/iOS/MMKV/MMKV/MMKV.mm index 3beadd03..7beddef1 100644 --- a/iOS/MMKV/MMKV/MMKV.mm +++ b/iOS/MMKV/MMKV/MMKV.mm @@ -1005,6 +1005,10 @@ - (BOOL)setDouble:(double)value forKey:(NSString *)key { return [self setData:data forKey:key]; } +- (BOOL)setString:(NSString *)value forKey:(NSString *)key { + return [self setObject:value forKey:key]; +} + - (id)getObjectOfClass:(Class)cls forKey:(NSString *)key { if (key.length <= 0) { return nil; @@ -1156,6 +1160,16 @@ - (double)getDoubleForKey:(NSString *)key defaultValue:(double)defaultValue { return defaultValue; } +- (nullable NSString *)getStringForKey:(NSString *)key { + return [self getStringForKey:key defaultValue:nil]; +} +- (nullable NSString *)getStringForKey:(NSString *)key defaultValue:(nullable id)defaultValue { + if (key.length <= 0) { + return defaultValue; + } + return [self getObjectOfClass:NSString.class forKey:key]; +} + #pragma mark - enumerate - (BOOL)containsKey:(NSString *)key { diff --git a/iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift b/iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift index 94355474..ab0b9051 100644 --- a/iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift +++ b/iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift @@ -48,7 +48,10 @@ class DemoSwiftUsage : NSObject { mmkv.set(Double.infinity, forKey: "double") print("Swift: double = \(mmkv.double(forKey: "double"))") - + + mmkv.set("Hello from Swift", forKey: "string") + print("Swift: string = \(mmkv.string(forKey: "string") ?? "")") + mmkv.set("Hello from Swift", forKey: "string") print("Swift: string = \(mmkv.object(of: NSString.self, forKey: "string") ?? "")") diff --git a/iOS/MMKVDemo/MMKVDemo/ViewController.mm b/iOS/MMKVDemo/MMKVDemo/ViewController.mm index 34ad6703..252d9e6d 100644 --- a/iOS/MMKVDemo/MMKVDemo/ViewController.mm +++ b/iOS/MMKVDemo/MMKVDemo/ViewController.mm @@ -83,7 +83,10 @@ - (void)funcionalTest { [mmkv setDouble:std::numeric_limits::max() forKey:@"double"]; NSLog(@"double:%f", [mmkv getDoubleForKey:@"double"]); - + + [mmkv setString:@"hello, mmkv" forKey:@"string"]; + NSLog(@"string:%@", [mmkv getStringForKey:@"string"]); + [mmkv setObject:@"hello, mmkv" forKey:@"string"]; NSLog(@"string:%@", [mmkv getObjectOfClass:NSString.class forKey:@"string"]); diff --git a/iOS/MMKVDemo/MMKVMacDemo/ViewController.mm b/iOS/MMKVDemo/MMKVMacDemo/ViewController.mm index b0e9cd13..00a71f30 100644 --- a/iOS/MMKVDemo/MMKVMacDemo/ViewController.mm +++ b/iOS/MMKVDemo/MMKVMacDemo/ViewController.mm @@ -61,7 +61,10 @@ - (void)funcionalTest { [mmkv setDouble:std::numeric_limits::max() forKey:@"double"]; NSLog(@"double:%f", [mmkv getDoubleForKey:@"double"]); - + + [mmkv setObject:@"hello, mmkv" forKey:@"string"]; + NSLog(@"string:%@", [mmkv getStringForKey:@"string"]); + [mmkv setObject:@"hello, mmkv" forKey:@"string"]; NSLog(@"string:%@", [mmkv getObjectOfClass:NSString.class forKey:@"string"]); From a4f2c93b9c185f7ec033fc0586d9f2bab739171e Mon Sep 17 00:00:00 2001 From: TL Date: Mon, 10 Dec 2018 14:59:07 +0800 Subject: [PATCH 08/16] update demo test case --- .../xcshareddata/IDEWorkspaceChecks.plist | 8 -------- iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm | 12 ++++++++++++ 2 files changed, 12 insertions(+), 8 deletions(-) delete mode 100644 iOS/MMKV/MMKV.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/iOS/MMKV/MMKV.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/iOS/MMKV/MMKV.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist deleted file mode 100644 index 18d98100..00000000 --- a/iOS/MMKV/MMKV.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +++ /dev/null @@ -1,8 +0,0 @@ - - - - - IDEDidComputeMac32BitWarning - - - diff --git a/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm b/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm index 88951178..a5542dce 100644 --- a/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm +++ b/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm @@ -213,6 +213,18 @@ - (void)testNSString { XCTAssertEqualObjects(value, nil); } +- (void)testNSStringForNewGetSet { + NSString *str = @"Hello 2018 world cup 世界杯"; + BOOL ret = [mmkv setString:str forKey:@"string"]; + XCTAssertEqual(ret, YES); + + NSString *value = [mmkv getStringForKey:@"string"]; + XCTAssertEqualObjects(value, str); + + value = [mmkv getStringForKey:KeyNotExist]; + XCTAssertEqualObjects(value, nil); +} + - (void)testNSData { NSString *str = @"Hello 2018 world cup 世界杯"; NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; From 9d523e5e3d563ca17ef64481d086ccfe7d0758eb Mon Sep 17 00:00:00 2001 From: guoling Date: Mon, 10 Dec 2018 17:08:48 +0800 Subject: [PATCH 09/16] store JavaVM instead --- .../MMKV/mmkv/src/main/cpp/native-bridge.cpp | 34 +++++++++++++------ .../com/tencent/mmkvdemo/MainActivity.java | 2 ++ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp b/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp index 43db597f..54a49193 100644 --- a/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp +++ b/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp @@ -32,9 +32,10 @@ static jclass g_cls = nullptr; static jfieldID g_fileID = nullptr; static jmethodID g_callbackOnCRCFailID = nullptr; static jmethodID g_callbackOnFileLengthErrorID = nullptr; -thread_local static JNIEnv *g_currentEnv = nullptr; +static JavaVM *g_currentJVM = nullptr; extern "C" JNIEXPORT JNICALL jint JNI_OnLoad(JavaVM *vm, void *reserved) { + g_currentJVM = vm; JNIEnv *env; if (vm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_6) != JNI_OK) { return -1; @@ -138,20 +139,35 @@ static jobjectArray vector2jarray(JNIEnv *env, const vector &arr) { return nullptr; } +static JNIEnv *getCurrentEnv() { + if (g_currentJVM) { + JNIEnv *currentEnv = nullptr; + auto ret = g_currentJVM->GetEnv(reinterpret_cast(¤tEnv), JNI_VERSION_1_6); + if (ret == JNI_OK) { + return currentEnv; + } else { + MMKVError("fail to get current JNIEnv: %d", ret); + } + } + return nullptr; +} + MMKVRecoverStrategic onMMKVCRCCheckFail(const std::string &mmapID) { - if (g_currentEnv && g_callbackOnCRCFailID) { - jstring str = string2jstring(g_currentEnv, mmapID); - auto strategic = g_currentEnv->CallStaticIntMethod(g_cls, g_callbackOnCRCFailID, str); + auto currentEnv = getCurrentEnv(); + if (g_currentJVM && g_callbackOnCRCFailID) { + jstring str = string2jstring(currentEnv, mmapID); + auto strategic = currentEnv->CallStaticIntMethod(g_cls, g_callbackOnCRCFailID, str); return static_cast(strategic); } return OnErrorDiscard; } MMKVRecoverStrategic onMMKVFileLengthError(const std::string &mmapID) { - if (g_currentEnv && g_callbackOnFileLengthErrorID) { - jstring str = string2jstring(g_currentEnv, mmapID); + auto currentEnv = getCurrentEnv(); + if (currentEnv && g_callbackOnFileLengthErrorID) { + jstring str = string2jstring(currentEnv, mmapID); auto strategic = - g_currentEnv->CallStaticIntMethod(g_cls, g_callbackOnFileLengthErrorID, str); + currentEnv->CallStaticIntMethod(g_cls, g_callbackOnFileLengthErrorID, str); return static_cast(strategic); } return OnErrorDiscard; @@ -159,7 +175,6 @@ MMKVRecoverStrategic onMMKVFileLengthError(const std::string &mmapID) { extern "C" JNIEXPORT JNICALL jlong Java_com_tencent_mmkv_MMKV_getMMKVWithID( JNIEnv *env, jobject obj, jstring mmapID, jint mode, jstring cryptKey) { - g_currentEnv = env; MMKV *kv = nullptr; if (!mmapID) { return (jlong) kv; @@ -181,7 +196,6 @@ extern "C" JNIEXPORT JNICALL jlong Java_com_tencent_mmkv_MMKV_getMMKVWithID( extern "C" JNIEXPORT JNICALL jlong Java_com_tencent_mmkv_MMKV_getMMKVWithIDAndSize( JNIEnv *env, jobject obj, jstring mmapID, jint size, jint mode, jstring cryptKey) { - g_currentEnv = env; MMKV *kv = nullptr; if (!mmapID || size < 0) { return (jlong) kv; @@ -204,7 +218,6 @@ extern "C" JNIEXPORT JNICALL jlong Java_com_tencent_mmkv_MMKV_getDefaultMMKV(JNI jobject obj, jint mode, jstring cryptKey) { - g_currentEnv = env; MMKV *kv = nullptr; if (cryptKey != nullptr) { @@ -222,7 +235,6 @@ extern "C" JNIEXPORT JNICALL jlong Java_com_tencent_mmkv_MMKV_getDefaultMMKV(JNI extern "C" JNIEXPORT JNICALL jlong Java_com_tencent_mmkv_MMKV_getMMKVWithAshmemFD( JNIEnv *env, jobject obj, jstring mmapID, jint fd, jint metaFD, jstring cryptKey) { - g_currentEnv = env; MMKV *kv = nullptr; if (!mmapID || fd < 0 || metaFD < 0) { return (jlong) kv; diff --git a/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java b/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java index c25db381..8ec6f5a9 100644 --- a/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java +++ b/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java @@ -69,6 +69,8 @@ public void onClick(View v) { } }); + //testHolderForMultiThread(); + //prepareInterProcessAshmem(); //prepareInterProcessAshmemByContentProvider(KEY_1); From 837c08d83eef2a20072da26143c688c10545fa72 Mon Sep 17 00:00:00 2001 From: TL Date: Tue, 11 Dec 2018 11:14:39 +0800 Subject: [PATCH 10/16] format code --- iOS/MMKVDemo/MMKVDemo/ViewController.mm | 8 ++++---- iOS/MMKVDemo/MMKVMacDemo/ViewController.mm | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/iOS/MMKVDemo/MMKVDemo/ViewController.mm b/iOS/MMKVDemo/MMKVDemo/ViewController.mm index 252d9e6d..02430d6e 100644 --- a/iOS/MMKVDemo/MMKVDemo/ViewController.mm +++ b/iOS/MMKVDemo/MMKVDemo/ViewController.mm @@ -83,10 +83,10 @@ - (void)funcionalTest { [mmkv setDouble:std::numeric_limits::max() forKey:@"double"]; NSLog(@"double:%f", [mmkv getDoubleForKey:@"double"]); - - [mmkv setString:@"hello, mmkv" forKey:@"string"]; - NSLog(@"string:%@", [mmkv getStringForKey:@"string"]); - + + [mmkv setString:@"hello, mmkv" forKey:@"string"]; + NSLog(@"string:%@", [mmkv getStringForKey:@"string"]); + [mmkv setObject:@"hello, mmkv" forKey:@"string"]; NSLog(@"string:%@", [mmkv getObjectOfClass:NSString.class forKey:@"string"]); diff --git a/iOS/MMKVDemo/MMKVMacDemo/ViewController.mm b/iOS/MMKVDemo/MMKVMacDemo/ViewController.mm index 00a71f30..39d61f13 100644 --- a/iOS/MMKVDemo/MMKVMacDemo/ViewController.mm +++ b/iOS/MMKVDemo/MMKVMacDemo/ViewController.mm @@ -61,10 +61,10 @@ - (void)funcionalTest { [mmkv setDouble:std::numeric_limits::max() forKey:@"double"]; NSLog(@"double:%f", [mmkv getDoubleForKey:@"double"]); - - [mmkv setObject:@"hello, mmkv" forKey:@"string"]; - NSLog(@"string:%@", [mmkv getStringForKey:@"string"]); - + + [mmkv setObject:@"hello, mmkv" forKey:@"string"]; + NSLog(@"string:%@", [mmkv getStringForKey:@"string"]); + [mmkv setObject:@"hello, mmkv" forKey:@"string"]; NSLog(@"string:%@", [mmkv getObjectOfClass:NSString.class forKey:@"string"]); From 51b391f09e303ab49ee05d66521a3053a2446816 Mon Sep 17 00:00:00 2001 From: TL Date: Tue, 11 Dec 2018 16:41:41 +0800 Subject: [PATCH 11/16] add get/set date function --- iOS/MMKV/MMKV/MMKV.h | 5 +++++ iOS/MMKV/MMKV/MMKV.mm | 14 ++++++++++++++ iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift | 10 +++++++--- iOS/MMKVDemo/MMKVDemo/ViewController.mm | 11 +++++++---- iOS/MMKVDemo/MMKVMacDemo/ViewController.mm | 13 ++++++++----- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/iOS/MMKV/MMKV/MMKV.h b/iOS/MMKV/MMKV/MMKV.h index ab47644a..b70f2ba2 100644 --- a/iOS/MMKV/MMKV/MMKV.h +++ b/iOS/MMKV/MMKV/MMKV.h @@ -58,6 +58,8 @@ NS_ASSUME_NONNULL_BEGIN - (BOOL)setString:(NSString *)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:)); +- (BOOL)setDate:(NSDate *)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:)); + - (nullable id)getObjectOfClass:(Class)cls forKey:(NSString *)key NS_SWIFT_NAME(object(of:forKey:)); - (BOOL)getBoolForKey:(NSString *)key __attribute__((swift_name("bool(forKey:)"))); @@ -84,6 +86,9 @@ NS_ASSUME_NONNULL_BEGIN - (nullable NSString *)getStringForKey:(NSString *)key NS_SWIFT_NAME(string(forKey:)); - (nullable NSString *)getStringForKey:(NSString *)key defaultValue:(nullable id)defaultValue NS_SWIFT_NAME(string(forKey:defaultValue:)); +- (nullable NSDate *)getDateForKey:(NSString *)key NS_SWIFT_NAME(date(forKey:)); +- (nullable NSDate *)getDateForKey:(NSString *)key defaultValue:(nullable id)defaultValue NS_SWIFT_NAME(date(forKey:defaultValue:)); + - (BOOL)containsKey:(NSString *)key NS_SWIFT_NAME(contains(key:)); - (size_t)count; diff --git a/iOS/MMKV/MMKV/MMKV.mm b/iOS/MMKV/MMKV/MMKV.mm index 7beddef1..2b2da570 100644 --- a/iOS/MMKV/MMKV/MMKV.mm +++ b/iOS/MMKV/MMKV/MMKV.mm @@ -1009,6 +1009,10 @@ - (BOOL)setString:(NSString *)value forKey:(NSString *)key { return [self setObject:value forKey:key]; } +- (BOOL)setDate:(NSDate *)value forKey:(NSString *)key { + return [self setObject:value forKey:key]; +} + - (id)getObjectOfClass:(Class)cls forKey:(NSString *)key { if (key.length <= 0) { return nil; @@ -1170,6 +1174,16 @@ - (nullable NSString *)getStringForKey:(NSString *)key defaultValue:(nullable id return [self getObjectOfClass:NSString.class forKey:key]; } +- (nullable NSDate *)getDateForKey:(NSString *)key { + return [self getDateForKey:key defaultValue:nil]; +} +- (nullable NSDate *)getDateForKey:(NSString *)key defaultValue:(nullable id)defaultValue { + if (key.length <= 0) { + return defaultValue; + } + return [self getObjectOfClass:NSDate.class forKey:key]; +} + #pragma mark - enumerate - (BOOL)containsKey:(NSString *)key { diff --git a/iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift b/iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift index ab0b9051..f0547b19 100644 --- a/iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift +++ b/iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift @@ -52,12 +52,16 @@ class DemoSwiftUsage : NSObject { mmkv.set("Hello from Swift", forKey: "string") print("Swift: string = \(mmkv.string(forKey: "string") ?? "")") - mmkv.set("Hello from Swift", forKey: "string") - print("Swift: string = \(mmkv.object(of: NSString.self, forKey: "string") ?? "")") +// mmkv.set("Hello from Swift", forKey: "string") +// print("Swift: string = \(mmkv.object(of: NSString.self, forKey: "string") ?? "")") mmkv.set(NSDate(), forKey: "date") - let date = mmkv.object(of: NSDate.self, forKey: "date") as? Date + let date = mmkv.date(forKey: "date") print("Swift: date = \(date?.description(with: .current) ?? "null")") + +// mmkv.set(NSDate(), forKey: "date") +// let date = mmkv.object(of: NSDate.self, forKey: "date") as? Date +// print("Swift: date = \(date?.description(with: .current) ?? "null")") mmkv.set("Hello from Swift".data(using: .utf8) ?? Data(), forKey: "data") let data = mmkv.object(of: NSData.self, forKey: "data") as? Data diff --git a/iOS/MMKVDemo/MMKVDemo/ViewController.mm b/iOS/MMKVDemo/MMKVDemo/ViewController.mm index 02430d6e..4f5379ae 100644 --- a/iOS/MMKVDemo/MMKVDemo/ViewController.mm +++ b/iOS/MMKVDemo/MMKVDemo/ViewController.mm @@ -87,8 +87,8 @@ - (void)funcionalTest { [mmkv setString:@"hello, mmkv" forKey:@"string"]; NSLog(@"string:%@", [mmkv getStringForKey:@"string"]); - [mmkv setObject:@"hello, mmkv" forKey:@"string"]; - NSLog(@"string:%@", [mmkv getObjectOfClass:NSString.class forKey:@"string"]); + // [mmkv setObject:@"hello, mmkv" forKey:@"string"]; + // NSLog(@"string:%@", [mmkv getObjectOfClass:NSString.class forKey:@"string"]); [mmkv setObject:nil forKey:@"string"]; NSLog(@"string after set nil:%@, containsKey:%d", @@ -96,8 +96,11 @@ - (void)funcionalTest { forKey:@"string"], [mmkv containsKey:@"string"]); - [mmkv setObject:[NSDate date] forKey:@"date"]; - NSLog(@"date:%@", [mmkv getObjectOfClass:NSDate.class forKey:@"date"]); + [mmkv setDate:[NSDate date] forKey:@"date"]; + NSLog(@"date:%@", [mmkv getDateForKey:@"date"]); + + // [mmkv setObject:[NSDate date] forKey:@"date"]; + // NSLog(@"date:%@", [mmkv getObjectOfClass:NSDate.class forKey:@"date"]); [mmkv setObject:[@"hello, mmkv again and again" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"data"]; NSData *data = [mmkv getObjectOfClass:NSData.class forKey:@"data"]; diff --git a/iOS/MMKVDemo/MMKVMacDemo/ViewController.mm b/iOS/MMKVDemo/MMKVMacDemo/ViewController.mm index 39d61f13..edbc013e 100644 --- a/iOS/MMKVDemo/MMKVMacDemo/ViewController.mm +++ b/iOS/MMKVDemo/MMKVMacDemo/ViewController.mm @@ -62,14 +62,17 @@ - (void)funcionalTest { [mmkv setDouble:std::numeric_limits::max() forKey:@"double"]; NSLog(@"double:%f", [mmkv getDoubleForKey:@"double"]); - [mmkv setObject:@"hello, mmkv" forKey:@"string"]; + [mmkv setString:@"hello, mmkv" forKey:@"string"]; NSLog(@"string:%@", [mmkv getStringForKey:@"string"]); - [mmkv setObject:@"hello, mmkv" forKey:@"string"]; - NSLog(@"string:%@", [mmkv getObjectOfClass:NSString.class forKey:@"string"]); + // [mmkv setObject:@"hello, mmkv" forKey:@"string"]; + // NSLog(@"string:%@", [mmkv getObjectOfClass:NSString.class forKey:@"string"]); - [mmkv setObject:[NSDate date] forKey:@"date"]; - NSLog(@"date:%@", [mmkv getObjectOfClass:NSDate.class forKey:@"date"]); + [mmkv setDate:[NSDate date] forKey:@"date"]; + NSLog(@"date:%@", [mmkv getDateForKey:@"date"]); + + // [mmkv setObject:[NSDate date] forKey:@"date"]; + // NSLog(@"date:%@", [mmkv getObjectOfClass:NSDate.class forKey:@"date"]); [mmkv setObject:[@"hello, mmkv again and again" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"data"]; NSData *data = [mmkv getObjectOfClass:NSData.class forKey:@"data"]; From a3ce973686976ff22cc7860cd4a32c2a201446f6 Mon Sep 17 00:00:00 2001 From: TL Date: Tue, 11 Dec 2018 17:10:47 +0800 Subject: [PATCH 12/16] update demo test case --- iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm b/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm index a5542dce..c81c6c91 100644 --- a/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm +++ b/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm @@ -250,6 +250,18 @@ - (void)testNSDate { XCTAssertEqualObjects(value, nil); } +- (void)testNSDateForNewGetSet { + NSDate *date = [NSDate date]; + BOOL ret = [mmkv setDate:date forKey:@"date"]; + XCTAssertEqual(ret, YES); + + NSDate *value = [mmkv getDateForKey:@"date"]; + XCTAssertEqualWithAccuracy(date.timeIntervalSince1970, value.timeIntervalSince1970, 0.001); + + value = [mmkv getObjectOfClass:NSDate.class forKey:KeyNotExist]; + XCTAssertEqualObjects(value, nil); +} + - (void)testNSDictionary { NSDictionary *dic = @{@"key1" : @"value1", @"key2" : @(2)}; From f0531763d2477cad9eefa4b2d51ea6f07e4d1bec Mon Sep 17 00:00:00 2001 From: guoling Date: Tue, 11 Dec 2018 21:55:03 +0800 Subject: [PATCH 13/16] format --- .../MMKV/mmkv/src/main/cpp/native-bridge.cpp | 5 ++- iOS/MMKV/MMKV/MMKV.mm | 24 ++++++------- iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm | 36 +++++++++---------- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp b/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp index 54a49193..2964f47a 100644 --- a/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp +++ b/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp @@ -142,7 +142,7 @@ static jobjectArray vector2jarray(JNIEnv *env, const vector &arr) { static JNIEnv *getCurrentEnv() { if (g_currentJVM) { JNIEnv *currentEnv = nullptr; - auto ret = g_currentJVM->GetEnv(reinterpret_cast(¤tEnv), JNI_VERSION_1_6); + auto ret = g_currentJVM->GetEnv(reinterpret_cast(¤tEnv), JNI_VERSION_1_6); if (ret == JNI_OK) { return currentEnv; } else { @@ -166,8 +166,7 @@ MMKVRecoverStrategic onMMKVFileLengthError(const std::string &mmapID) { auto currentEnv = getCurrentEnv(); if (currentEnv && g_callbackOnFileLengthErrorID) { jstring str = string2jstring(currentEnv, mmapID); - auto strategic = - currentEnv->CallStaticIntMethod(g_cls, g_callbackOnFileLengthErrorID, str); + auto strategic = currentEnv->CallStaticIntMethod(g_cls, g_callbackOnFileLengthErrorID, str); return static_cast(strategic); } return OnErrorDiscard; diff --git a/iOS/MMKV/MMKV/MMKV.mm b/iOS/MMKV/MMKV/MMKV.mm index 2b2da570..80e63f28 100644 --- a/iOS/MMKV/MMKV/MMKV.mm +++ b/iOS/MMKV/MMKV/MMKV.mm @@ -1006,11 +1006,11 @@ - (BOOL)setDouble:(double)value forKey:(NSString *)key { } - (BOOL)setString:(NSString *)value forKey:(NSString *)key { - return [self setObject:value forKey:key]; + return [self setObject:value forKey:key]; } - (BOOL)setDate:(NSDate *)value forKey:(NSString *)key { - return [self setObject:value forKey:key]; + return [self setObject:value forKey:key]; } - (id)getObjectOfClass:(Class)cls forKey:(NSString *)key { @@ -1165,23 +1165,23 @@ - (double)getDoubleForKey:(NSString *)key defaultValue:(double)defaultValue { } - (nullable NSString *)getStringForKey:(NSString *)key { - return [self getStringForKey:key defaultValue:nil]; + return [self getStringForKey:key defaultValue:nil]; } - (nullable NSString *)getStringForKey:(NSString *)key defaultValue:(nullable id)defaultValue { - if (key.length <= 0) { - return defaultValue; - } - return [self getObjectOfClass:NSString.class forKey:key]; + if (key.length <= 0) { + return defaultValue; + } + return [self getObjectOfClass:NSString.class forKey:key]; } - (nullable NSDate *)getDateForKey:(NSString *)key { - return [self getDateForKey:key defaultValue:nil]; + return [self getDateForKey:key defaultValue:nil]; } - (nullable NSDate *)getDateForKey:(NSString *)key defaultValue:(nullable id)defaultValue { - if (key.length <= 0) { - return defaultValue; - } - return [self getObjectOfClass:NSDate.class forKey:key]; + if (key.length <= 0) { + return defaultValue; + } + return [self getObjectOfClass:NSDate.class forKey:key]; } #pragma mark - enumerate diff --git a/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm b/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm index c81c6c91..b83ed833 100644 --- a/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm +++ b/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm @@ -214,15 +214,15 @@ - (void)testNSString { } - (void)testNSStringForNewGetSet { - NSString *str = @"Hello 2018 world cup 世界杯"; - BOOL ret = [mmkv setString:str forKey:@"string"]; - XCTAssertEqual(ret, YES); - - NSString *value = [mmkv getStringForKey:@"string"]; - XCTAssertEqualObjects(value, str); - - value = [mmkv getStringForKey:KeyNotExist]; - XCTAssertEqualObjects(value, nil); + NSString *str = @"Hello 2018 world cup 世界杯"; + BOOL ret = [mmkv setString:str forKey:@"string"]; + XCTAssertEqual(ret, YES); + + NSString *value = [mmkv getStringForKey:@"string"]; + XCTAssertEqualObjects(value, str); + + value = [mmkv getStringForKey:KeyNotExist]; + XCTAssertEqualObjects(value, nil); } - (void)testNSData { @@ -251,15 +251,15 @@ - (void)testNSDate { } - (void)testNSDateForNewGetSet { - NSDate *date = [NSDate date]; - BOOL ret = [mmkv setDate:date forKey:@"date"]; - XCTAssertEqual(ret, YES); - - NSDate *value = [mmkv getDateForKey:@"date"]; - XCTAssertEqualWithAccuracy(date.timeIntervalSince1970, value.timeIntervalSince1970, 0.001); - - value = [mmkv getObjectOfClass:NSDate.class forKey:KeyNotExist]; - XCTAssertEqualObjects(value, nil); + NSDate *date = [NSDate date]; + BOOL ret = [mmkv setDate:date forKey:@"date"]; + XCTAssertEqual(ret, YES); + + NSDate *value = [mmkv getDateForKey:@"date"]; + XCTAssertEqualWithAccuracy(date.timeIntervalSince1970, value.timeIntervalSince1970, 0.001); + + value = [mmkv getObjectOfClass:NSDate.class forKey:KeyNotExist]; + XCTAssertEqualObjects(value, nil); } - (void)testNSDictionary { From 9d086b6a13fa77cd48fb658c4525860d8ac9c28f Mon Sep 17 00:00:00 2001 From: guoling Date: Tue, 11 Dec 2018 22:17:01 +0800 Subject: [PATCH 14/16] setData & getData --- iOS/MMKV/MMKV/MMKV.h | 12 +++-- iOS/MMKV/MMKV/MMKV.mm | 58 ++++++++++++++-------- iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift | 11 +--- iOS/MMKVDemo/MMKVDemo/ViewController.mm | 10 +--- 4 files changed, 48 insertions(+), 43 deletions(-) diff --git a/iOS/MMKV/MMKV/MMKV.h b/iOS/MMKV/MMKV/MMKV.h index b70f2ba2..5460b11a 100644 --- a/iOS/MMKV/MMKV/MMKV.h +++ b/iOS/MMKV/MMKV/MMKV.h @@ -39,8 +39,7 @@ NS_ASSUME_NONNULL_BEGIN - (BOOL)reKey:(nullable NSData *)newKey NS_SWIFT_NAME(reset(cryptKey:)); - (nullable NSData *)cryptKey; -// object: NSString/NSData/NSDate/id -- (BOOL)setObject:(nullable id)object forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:)); +- (BOOL)setObject:(nullable NSObject *)object forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:)); - (BOOL)setBool:(BOOL)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:)); @@ -60,6 +59,8 @@ NS_ASSUME_NONNULL_BEGIN - (BOOL)setDate:(NSDate *)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:)); +- (BOOL)setData:(NSData *)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:)); + - (nullable id)getObjectOfClass:(Class)cls forKey:(NSString *)key NS_SWIFT_NAME(object(of:forKey:)); - (BOOL)getBoolForKey:(NSString *)key __attribute__((swift_name("bool(forKey:)"))); @@ -84,10 +85,13 @@ NS_ASSUME_NONNULL_BEGIN - (double)getDoubleForKey:(NSString *)key defaultValue:(double)defaultValue NS_SWIFT_NAME(double(forKey:defaultValue:)); - (nullable NSString *)getStringForKey:(NSString *)key NS_SWIFT_NAME(string(forKey:)); -- (nullable NSString *)getStringForKey:(NSString *)key defaultValue:(nullable id)defaultValue NS_SWIFT_NAME(string(forKey:defaultValue:)); +- (nullable NSString *)getStringForKey:(NSString *)key defaultValue:(nullable NSString *)defaultValue NS_SWIFT_NAME(string(forKey:defaultValue:)); - (nullable NSDate *)getDateForKey:(NSString *)key NS_SWIFT_NAME(date(forKey:)); -- (nullable NSDate *)getDateForKey:(NSString *)key defaultValue:(nullable id)defaultValue NS_SWIFT_NAME(date(forKey:defaultValue:)); +- (nullable NSDate *)getDateForKey:(NSString *)key defaultValue:(nullable NSDate *)defaultValue NS_SWIFT_NAME(date(forKey:defaultValue:)); + +- (nullable NSData *)getDataForKey:(NSString *)key NS_SWIFT_NAME(data(forKey:)); +- (nullable NSData *)getDataForKey:(NSString *)key defaultValue:(nullable NSData *)defaultValue NS_SWIFT_NAME(data(forKey:defaultValue:)); - (BOOL)containsKey:(NSString *)key NS_SWIFT_NAME(contains(key:)); diff --git a/iOS/MMKV/MMKV/MMKV.mm b/iOS/MMKV/MMKV/MMKV.mm index 80e63f28..512e5c58 100644 --- a/iOS/MMKV/MMKV/MMKV.mm +++ b/iOS/MMKV/MMKV/MMKV.mm @@ -619,7 +619,7 @@ - (BOOL)writeAcutalSize:(size_t)actualSize { return YES; } -- (BOOL)setData:(NSData *)data forKey:(NSString *)key { +- (BOOL)setRawData:(NSData *)data forKey:(NSString *)key { if (data.length <= 0 || key.length <= 0) { return NO; } @@ -682,7 +682,7 @@ - (BOOL)appendData:(NSData *)data forKey:(NSString *)key { } } -- (NSData *)getDataForKey:(NSString *)key { +- (NSData *)getRawDataForKey:(NSString *)key { CScopedLock lock(m_lock); [self checkLoadData]; return [m_dic objectForKey:key]; @@ -900,7 +900,7 @@ - (BOOL)reKey:(NSData *)newKey { #pragma mark - set & get -- (BOOL)setObject:(nullable id)object forKey:(NSString *)key { +- (BOOL)setObject:(nullable NSObject *)object forKey:(NSString *)key { if (key.length <= 0) { return NO; } @@ -913,12 +913,12 @@ - (BOOL)setObject:(nullable id)object forKey:(NSString *)key { if ([MiniPBCoder isMiniPBCoderCompatibleObject:object]) { data = [MiniPBCoder encodeDataWithObject:object]; } else { - if ([[object class] conformsToProtocol:@protocol(NSCoding)]) { + /*if ([object conformsToProtocol:@protocol(NSCoding)])*/ { data = [NSKeyedArchiver archivedDataWithRootObject:object]; } } - return [self setData:data forKey:key]; + return [self setRawData:data forKey:key]; } - (BOOL)setBool:(BOOL)value forKey:(NSString *)key { @@ -930,7 +930,7 @@ - (BOOL)setBool:(BOOL)value forKey:(NSString *)key { MiniCodedOutputData output(data); output.writeBool(value); - return [self setData:data forKey:key]; + return [self setRawData:data forKey:key]; } - (BOOL)setInt32:(int32_t)value forKey:(NSString *)key { @@ -942,7 +942,7 @@ - (BOOL)setInt32:(int32_t)value forKey:(NSString *)key { MiniCodedOutputData output(data); output.writeInt32(value); - return [self setData:data forKey:key]; + return [self setRawData:data forKey:key]; } - (BOOL)setUInt32:(uint32_t)value forKey:(NSString *)key { @@ -954,7 +954,7 @@ - (BOOL)setUInt32:(uint32_t)value forKey:(NSString *)key { MiniCodedOutputData output(data); output.writeUInt32(value); - return [self setData:data forKey:key]; + return [self setRawData:data forKey:key]; } - (BOOL)setInt64:(int64_t)value forKey:(NSString *)key { @@ -966,7 +966,7 @@ - (BOOL)setInt64:(int64_t)value forKey:(NSString *)key { MiniCodedOutputData output(data); output.writeInt64(value); - return [self setData:data forKey:key]; + return [self setRawData:data forKey:key]; } - (BOOL)setUInt64:(uint64_t)value forKey:(NSString *)key { @@ -978,7 +978,7 @@ - (BOOL)setUInt64:(uint64_t)value forKey:(NSString *)key { MiniCodedOutputData output(data); output.writeUInt64(value); - return [self setData:data forKey:key]; + return [self setRawData:data forKey:key]; } - (BOOL)setFloat:(float)value forKey:(NSString *)key { @@ -990,7 +990,7 @@ - (BOOL)setFloat:(float)value forKey:(NSString *)key { MiniCodedOutputData output(data); output.writeFloat(value); - return [self setData:data forKey:key]; + return [self setRawData:data forKey:key]; } - (BOOL)setDouble:(double)value forKey:(NSString *)key { @@ -1002,7 +1002,7 @@ - (BOOL)setDouble:(double)value forKey:(NSString *)key { MiniCodedOutputData output(data); output.writeDouble(value); - return [self setData:data forKey:key]; + return [self setRawData:data forKey:key]; } - (BOOL)setString:(NSString *)value forKey:(NSString *)key { @@ -1013,11 +1013,15 @@ - (BOOL)setDate:(NSDate *)value forKey:(NSString *)key { return [self setObject:value forKey:key]; } +- (BOOL)setData:(NSData *)value forKey:(NSString *)key { + return [self setObject:value forKey:key]; +} + - (id)getObjectOfClass:(Class)cls forKey:(NSString *)key { if (key.length <= 0) { return nil; } - NSData *data = [self getDataForKey:key]; + NSData *data = [self getRawDataForKey:key]; if (data.length > 0) { if ([MiniPBCoder isMiniPBCoderCompatibleType:cls]) { @@ -1038,7 +1042,7 @@ - (BOOL)getBoolForKey:(NSString *)key defaultValue:(BOOL)defaultValue { if (key.length <= 0) { return defaultValue; } - NSData *data = [self getDataForKey:key]; + NSData *data = [self getRawDataForKey:key]; if (data.length > 0) { @try { MiniCodedInputData input(data); @@ -1057,7 +1061,7 @@ - (int32_t)getInt32ForKey:(NSString *)key defaultValue:(int32_t)defaultValue { if (key.length <= 0) { return defaultValue; } - NSData *data = [self getDataForKey:key]; + NSData *data = [self getRawDataForKey:key]; if (data.length > 0) { @try { MiniCodedInputData input(data); @@ -1076,7 +1080,7 @@ - (uint32_t)getUInt32ForKey:(NSString *)key defaultValue:(uint32_t)defaultValue if (key.length <= 0) { return defaultValue; } - NSData *data = [self getDataForKey:key]; + NSData *data = [self getRawDataForKey:key]; if (data.length > 0) { @try { MiniCodedInputData input(data); @@ -1095,7 +1099,7 @@ - (int64_t)getInt64ForKey:(NSString *)key defaultValue:(int64_t)defaultValue { if (key.length <= 0) { return defaultValue; } - NSData *data = [self getDataForKey:key]; + NSData *data = [self getRawDataForKey:key]; if (data.length > 0) { @try { MiniCodedInputData input(data); @@ -1114,7 +1118,7 @@ - (uint64_t)getUInt64ForKey:(NSString *)key defaultValue:(uint64_t)defaultValue if (key.length <= 0) { return defaultValue; } - NSData *data = [self getDataForKey:key]; + NSData *data = [self getRawDataForKey:key]; if (data.length > 0) { @try { MiniCodedInputData input(data); @@ -1133,7 +1137,7 @@ - (float)getFloatForKey:(NSString *)key defaultValue:(float)defaultValue { if (key.length <= 0) { return defaultValue; } - NSData *data = [self getDataForKey:key]; + NSData *data = [self getRawDataForKey:key]; if (data.length > 0) { @try { MiniCodedInputData input(data); @@ -1152,7 +1156,7 @@ - (double)getDoubleForKey:(NSString *)key defaultValue:(double)defaultValue { if (key.length <= 0) { return defaultValue; } - NSData *data = [self getDataForKey:key]; + NSData *data = [self getRawDataForKey:key]; if (data.length > 0) { @try { MiniCodedInputData input(data); @@ -1167,7 +1171,7 @@ - (double)getDoubleForKey:(NSString *)key defaultValue:(double)defaultValue { - (nullable NSString *)getStringForKey:(NSString *)key { return [self getStringForKey:key defaultValue:nil]; } -- (nullable NSString *)getStringForKey:(NSString *)key defaultValue:(nullable id)defaultValue { +- (nullable NSString *)getStringForKey:(NSString *)key defaultValue:(nullable NSString *)defaultValue { if (key.length <= 0) { return defaultValue; } @@ -1177,13 +1181,23 @@ - (nullable NSString *)getStringForKey:(NSString *)key defaultValue:(nullable id - (nullable NSDate *)getDateForKey:(NSString *)key { return [self getDateForKey:key defaultValue:nil]; } -- (nullable NSDate *)getDateForKey:(NSString *)key defaultValue:(nullable id)defaultValue { +- (nullable NSDate *)getDateForKey:(NSString *)key defaultValue:(nullable NSDate *)defaultValue { if (key.length <= 0) { return defaultValue; } return [self getObjectOfClass:NSDate.class forKey:key]; } +- (nullable NSData *)getDataForKey:(NSString *)key { + return [self getDataForKey:key defaultValue:nil]; +} +- (nullable NSData *)getDataForKey:(NSString *)key defaultValue:(nullable NSData *)defaultValue { + if (key.length <= 0) { + return defaultValue; + } + return [self getObjectOfClass:NSData.class forKey:key]; +} + #pragma mark - enumerate - (BOOL)containsKey:(NSString *)key { diff --git a/iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift b/iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift index f0547b19..1eae2b55 100644 --- a/iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift +++ b/iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift @@ -51,20 +51,13 @@ class DemoSwiftUsage : NSObject { mmkv.set("Hello from Swift", forKey: "string") print("Swift: string = \(mmkv.string(forKey: "string") ?? "")") - -// mmkv.set("Hello from Swift", forKey: "string") -// print("Swift: string = \(mmkv.object(of: NSString.self, forKey: "string") ?? "")") - + mmkv.set(NSDate(), forKey: "date") let date = mmkv.date(forKey: "date") print("Swift: date = \(date?.description(with: .current) ?? "null")") -// mmkv.set(NSDate(), forKey: "date") -// let date = mmkv.object(of: NSDate.self, forKey: "date") as? Date -// print("Swift: date = \(date?.description(with: .current) ?? "null")") - mmkv.set("Hello from Swift".data(using: .utf8) ?? Data(), forKey: "data") - let data = mmkv.object(of: NSData.self, forKey: "data") as? Data + let data = mmkv.data(forKey: "data") let str = String(data: data ?? Data(), encoding: .utf8) ?? "" print("Swift: data = \(str)") diff --git a/iOS/MMKVDemo/MMKVDemo/ViewController.mm b/iOS/MMKVDemo/MMKVDemo/ViewController.mm index 4f5379ae..07d5bdc4 100644 --- a/iOS/MMKVDemo/MMKVDemo/ViewController.mm +++ b/iOS/MMKVDemo/MMKVDemo/ViewController.mm @@ -87,9 +87,6 @@ - (void)funcionalTest { [mmkv setString:@"hello, mmkv" forKey:@"string"]; NSLog(@"string:%@", [mmkv getStringForKey:@"string"]); - // [mmkv setObject:@"hello, mmkv" forKey:@"string"]; - // NSLog(@"string:%@", [mmkv getObjectOfClass:NSString.class forKey:@"string"]); - [mmkv setObject:nil forKey:@"string"]; NSLog(@"string after set nil:%@, containsKey:%d", [mmkv getObjectOfClass:NSString.class @@ -99,11 +96,8 @@ - (void)funcionalTest { [mmkv setDate:[NSDate date] forKey:@"date"]; NSLog(@"date:%@", [mmkv getDateForKey:@"date"]); - // [mmkv setObject:[NSDate date] forKey:@"date"]; - // NSLog(@"date:%@", [mmkv getObjectOfClass:NSDate.class forKey:@"date"]); - - [mmkv setObject:[@"hello, mmkv again and again" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"data"]; - NSData *data = [mmkv getObjectOfClass:NSData.class forKey:@"data"]; + [mmkv setData:[@"hello, mmkv again and again" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"data"]; + NSData *data = [mmkv getDataForKey:@"data"]; NSLog(@"data:%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); [mmkv removeValueForKey:@"bool"]; From af9f932ae81a492d18e9ba507ff9dde4ac526754 Mon Sep 17 00:00:00 2001 From: TL Date: Wed, 12 Dec 2018 14:31:38 +0800 Subject: [PATCH 15/16] update mmkv demo test case --- iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm b/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm index b83ed833..a8de46fe 100644 --- a/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm +++ b/iOS/MMKVDemo/MMKVDemoTests/MMKVDemoTests.mm @@ -238,6 +238,19 @@ - (void)testNSData { XCTAssertEqualObjects(value, nil); } +- (void)testNSDataForNewGetSet { + NSString *str = @"Hello 2018 world cup 世界杯"; + NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; + BOOL ret = [mmkv setData:data forKey:@"data"]; + XCTAssertEqual(ret, YES); + + NSData *value = [mmkv getDataForKey:@"data"]; + XCTAssertEqualObjects(value, data); + + value = [mmkv getDataForKey:KeyNotExist]; + XCTAssertEqualObjects(value, nil); +} + - (void)testNSDate { NSDate *date = [NSDate date]; BOOL ret = [mmkv setObject:date forKey:@"date"]; From 2b7aa79c5e65fcfb9501b588033cc18af1d7083d Mon Sep 17 00:00:00 2001 From: guoling Date: Fri, 14 Dec 2018 11:19:57 +0800 Subject: [PATCH 16/16] prepare for v1.0.15 --- Android/MMKV/gradle.properties | 2 +- .../src/main/java/com/tencent/mmkv/MMKV.java | 3 +-- Android/MMKV/mmkvdemo/build.gradle | 6 +++--- CHANGELOG.md | 17 +++++++++++++++++ README.md | 6 +++--- iOS/MMKV/MMKV/Resources/Info.plist | 2 +- readme_cn.md | 4 ++-- 7 files changed, 28 insertions(+), 12 deletions(-) diff --git a/Android/MMKV/gradle.properties b/Android/MMKV/gradle.properties index 17ceb0d4..489d64f9 100644 --- a/Android/MMKV/gradle.properties +++ b/Android/MMKV/gradle.properties @@ -12,6 +12,6 @@ org.gradle.jvmargs=-Xmx1536m # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true -VERSION_NAME_PREFIX=1.0.14 +VERSION_NAME_PREFIX=1.0.15 #VERSION_NAME_SUFFIX=-SNAPSHOT VERSION_NAME_SUFFIX= \ No newline at end of file diff --git a/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java b/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java index 460b431a..94decd0e 100644 --- a/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java +++ b/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java @@ -304,8 +304,7 @@ public T decodeParcelable(String key, Class tClass) { } @SuppressWarnings("unchecked") - public - T decodeParcelable(String key, Class tClass, T defaultValue) { + public T decodeParcelable(String key, Class tClass, T defaultValue) { if (tClass == null) { return defaultValue; } diff --git a/Android/MMKV/mmkvdemo/build.gradle b/Android/MMKV/mmkvdemo/build.gradle index c55f2718..9bfb29cd 100644 --- a/Android/MMKV/mmkvdemo/build.gradle +++ b/Android/MMKV/mmkvdemo/build.gradle @@ -62,9 +62,9 @@ repositories { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation project(':mmkv') -// implementation 'com.tencent:mmkv:1.0.14' -// implementation 'com.tencent:mmkv-static:1.0.14' +// implementation project(':mmkv') + implementation 'com.tencent:mmkv:1.0.15' +// implementation 'com.tencent:mmkv-static:1.0.15' implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' diff --git a/CHANGELOG.md b/CHANGELOG.md index 23ec9075..5d24c000 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,22 @@ # MMKV Change Log +## v1.0.15 / 2018-12-13 +### iOS / macOS +What's new + +* Storing **NSString/NSData/NSDate** directly by calling `setString`/`getSring`, `setData`/`getData`, `setDate`/getDate`. +* Fix a potencial crash due to divided by zero. + + +### Android +What's new + +* Fix a stack overflow crash due to the **callback** feature introduced by v1.0.13. +* Fix a potencial crash due to divided by zero. + +### Win32 +MMKV for Win32 in under construction. Hopefully will come out in next release. For those who are interested, check out branch `dev_win32` for the latest development. + ## v1.0.14 / 2018-11-30 ### iOS / macOS What's new diff --git a/README.md b/README.md index 9da33dd1..d7ab1627 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ [![license](https://img.shields.io/badge/license-BSD_3-brightgreen.svg?style=flat)](https://github.com/Tencent/MMKV/blob/master/LICENSE.TXT) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/MMKV/pulls) -[![Release Version](https://img.shields.io/badge/release-1.0.14-brightgreen.svg)](https://github.com/Tencent/MMKV/releases) +[![Release Version](https://img.shields.io/badge/release-1.0.15-brightgreen.svg)](https://github.com/Tencent/MMKV/releases) [![Platform](https://img.shields.io/badge/Platform-%20iOS%20%7C%20Android-brightgreen.svg)](https://github.com/Tencent/MMKV/wiki/home) 中文版本请参看[这里](./readme_cn.md) @@ -75,8 +75,8 @@ Add the following lines to `build.gradle` on your app module: ```gradle dependencies { - implementation 'com.tencent:mmkv:1.0.14' - // replace "1.0.14" with any available version + implementation 'com.tencent:mmkv:1.0.15' + // replace "1.0.15" with any available version } ``` diff --git a/iOS/MMKV/MMKV/Resources/Info.plist b/iOS/MMKV/MMKV/Resources/Info.plist index b84a849d..910e8276 100644 --- a/iOS/MMKV/MMKV/Resources/Info.plist +++ b/iOS/MMKV/MMKV/Resources/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.0.14 + 1.0.15 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/readme_cn.md b/readme_cn.md index 9512227a..ce77bf1a 100644 --- a/readme_cn.md +++ b/readme_cn.md @@ -59,8 +59,8 @@ NSString *str = [mmkv getObjectOfClass:NSString.class forKey:@"string"]; ```gradle dependencies { - implementation 'com.tencent:mmkv:1.0.14' - // replace "1.0.14" with any available version + implementation 'com.tencent:mmkv:1.0.15' + // replace "1.0.15" with any available version } ```