Solved

Updating user profile properties order issue

  • 3 October 2020
  • 3 replies
  • 11936 views

Hello,

I was working on the integration of Mixpanel push notifications in an ANDROID app and the requirement was :

Whenever a user is signed in, we need to clear all previous registered device push tokens assigned to user profile and replace them with the current device push token.

 

So in order to achieve that I wrote the following code:


// Clear all Android push tokens
mixPanelAPI.getPeople().clearPushRegistrationId();
// Clear the iOS push tokens (by unset the ios_devices property)
mixPanelAPI.getPeople().unset("$ios_devices");
// Assign the device push token to the user profile
getDevicePushToken(token -> {
// Register the current push token
mixPanelAPI.getPeople().setPushRegistrationId(token);
});

 

I tested the code above and it’s working fine BUT not in all devices.

In some devices the clearPushRegistrationId is being called or taking effect right after the call of setPushRegistrationId and instead of having the following order:

 

Clearing the Android push tokens → Set the new registration push Token

 

We end up having:

Set the new registration push Token → Clearing the Android push tokens

 

And this behavior leaves the user profile with no device push tokens.

Is there anyway to fix that issue?

icon

Best answer by argenis 21 October 2020, 13:40

Hey @zakbk95 

That’s indeed a race condition as the transactions are processed in parallel and in some cases the setting could be registered before the clearing.

A good way around it if you want to completely clear the values you had before and save the new one is to skip clearing and saving the new one, and just send one operation to overwrite the list itself. Something like:

JSONArray pushTokens = new JSONArray();
pushTokens.put("examplePushToken");
mixpanel.getPeople().set("$android_devices", pushTokens);

While you usually use $union operations to interact with list properties, if you send a set operation with an array, the array will be recreated with the value you are sending.

 

Hope that helps.

View original

3 replies

Badge

Hey @zakbk95 

That’s indeed a race condition as the transactions are processed in parallel and in some cases the setting could be registered before the clearing.

A good way around it if you want to completely clear the values you had before and save the new one is to skip clearing and saving the new one, and just send one operation to overwrite the list itself. Something like:

JSONArray pushTokens = new JSONArray();
pushTokens.put("examplePushToken");
mixpanel.getPeople().set("$android_devices", pushTokens);

While you usually use $union operations to interact with list properties, if you send a set operation with an array, the array will be recreated with the value you are sending.

 

Hope that helps.

Hey @zakbk95 

That’s indeed a race condition as the transactions are processed in parallel and in some cases the setting could be registered before the clearing.

A good way around it if you want to completely clear the values you had before and save the new one is to skip clearing and saving the new one, and just send one operation to overwrite the list itself. Something like:

JSONArray pushTokens = new JSONArray();
pushTokens.put("examplePushToken");
mixpanel.getPeople().set("$android_devices", pushTokens);

While you usually use $union operations to interact with list properties, if you send a set operation with an array, the array will be recreated with the value you are sending.

 

Hope that helps.

 

  

I’ve already done that in project. I figured out that we can only assign a JSON array that has only one token to the $android_devices property.

Thanks @argenis for the confirmation.

Userlevel 6
Badge +3

Thank you for following up @zakbk95!

Reply


Mixpanel