Page MenuHomePhabricator

Add getter and setter for integers, getter for all keys and function to remove subset of keys to MMKV
ClosedPublic

Authored by marcin on Feb 12 2024, 9:12 AM.
Tags
None
Referenced Files
Unknown Object (File)
Wed, May 8, 1:59 PM
Unknown Object (File)
Wed, May 8, 10:02 AM
Unknown Object (File)
Tue, May 7, 7:22 PM
Unknown Object (File)
Tue, May 7, 2:14 PM
Unknown Object (File)
Tue, May 7, 9:36 AM
Unknown Object (File)
Tue, May 7, 4:16 AM
Unknown Object (File)
Tue, May 7, 3:59 AM
Unknown Object (File)
Apr 9 2024, 1:24 AM
Subscribers

Details

Summary

This differential extends MMKV API with integer getter and setter, function to retrieve all keys in the db and function to remove subset of keys from the db.

Test Plan

iOS:

  1. Below the comm::CommMMKV::initialize() line add lines that set several different integers under different keys.
  2. In NotificationService.mm at the beginning add lines that:
    1. Get all keys.
    2. Tries to get value for each key.
  3. Then call remove with one-element array with one key.
  4. Get all keys again and log them.
  5. Send notification and check that all integers are logged and the set of keys logged doesn't contain the one passed to removeKeys.

Android:

  1. Below the CommMMKV.initialize() line add lines that set several different integers under different keys.
  2. In CommNotificationsHandler.java at the beginning add lines that:
    1. Get all keys.
    2. Tries to get value for each key.
  3. Then call remove with one-element array with one key.
  4. Get all keys again and log them.
  5. Send notification and check that all integers are logged and the set of keys logged doesn't contain the one passed to removeKeys.

Diff Detail

Repository
rCOMM Comm
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

native/cpp/CommonCpp/Tools/CommMMKV.h
17 ↗(On Diff #37009)

MMKV API can't return null when we try to get integer that doesn't exist. It allows us to set default value that is returned instead in case the integer isn't present. So my idea here is that the developer should pass as noValue the value that they believe should never be set under certain key. For example if we use this for unreadCount we could pass -1 since unread count is never negative. Our implementation passes this as default value and if MMKV returns default value then our implementation returns nullish integer (null in Java and std::nullopt in C++ and Objective-C).

Add method to remove subset of keys

marcin retitled this revision from Add getter and setter for integers and getter for all keys to MMKV to Add getter and setter for integers, getter for all keys and function to remove subset of keys to MMKV.Feb 13 2024, 1:45 AM
marcin edited the summary of this revision. (Show Details)
marcin edited the test plan for this revision. (Show Details)
tomek added inline comments.
native/android/app/src/cpp/CommMMKV.cpp
83 ↗(On Diff #37020)

What is the result of make_jstring? Are we dereferencing a pointer? Should we free the memory at the end?

85 ↗(On Diff #37020)

Why can the setString method accept std::string but removeKeys need to transform the parameters using make_jstring?

native/cpp/CommonCpp/Tools/CommMMKV.h
17 ↗(On Diff #37009)

Can we explain it in a code comment?

This revision is now accepted and ready to land.Feb 14 2024, 8:55 AM

Update iOS code to fix reentrancy issues with the NSE

Refactor to match changes in parent differential

native/android/app/src/cpp/CommMMKV.cpp
83 ↗(On Diff #37020)

What is the result of make_jstring?

Calling make_jstring returns local_ref.. See here.

Are we dereferencing a pointer? Should we free the memory at the end?

The following fbjni doc explains that underlying objects are destructed when reference is destructed. Additionally this docs states explicitly that local_ref follows reference counting destruction paradigm.

85 ↗(On Diff #37020)

Why can the setString method accept std::string

This fbjni doc states explicitely that C++ methods calling Java can work with both std::string and JString.

but removeKeys need to transform the parameters

There is no similar conversion from std::vector<T> to JArrayClass<T> unfortunately. Working with arrays is explained here.

using make_jstring?

The setElement method of JArrayClass<T> expects that we are setting an object that is plain JNI reference.

Add comment explaining how to use getInt in CommMMKV.

native/cpp/CommonCpp/Tools/CommMMKV.h
25 ↗(On Diff #37925)

Explanation of noValue argument.