React-native-code-push: Can't figure out the version user is currently running.

Created on 23 Jun 2016  路  10Comments  路  Source: microsoft/react-native-code-push

Hi,
I tried in several ways, but couldn't get a stable label of the update user is currently running.
Not downloading, not updating, running.

The flow should be like this:

PSUEDO

Check if an update exists -->
--- If update exists -->
--- --- check if the user has the right Android / iOS version code to download the update --> get current RUNNING CodePush label  --> display it --> sync().

--- If it doesnt exist -->
--- --- get current RUNNING CodePush label --> display it.

I tried to use the following code but got unstable results (sometimes the right version, sometimes empty object).

CodePush.getUpdateMetadata(CodePush.UpdateState.RUNNING).then((update) => {
    console.log(update.label);
});
enhancement

Most helpful comment

@avishayil What do you think about us adding a new method called getAppVersion (or something similar) that would return the version of the binary, with the running CodePush label appended to it (if applicable)? This way you could simply call this method and use the return type in all circumstances?

// This would return "1.0.0" if the app is running the binary, and
// "1.0.0.1", "1.0.0.2", etc. if the app was running a CodePush update
const version = await codePush.getAppVersion();

// Same behavior as above, but would concat the binary version and
// CodePush update with a hyphen instead of a period (e.g. "1.0.0-1").
const version = await codePush.getAppVersion("-");

If possible, I'd prefer to keep the behavior of getUpdateMetadata as it is, as opposed to trying to overload it to expose binary information as well.

If the above solution looks interesting to you, you could actually implement it on your end in the meantime (or customize it), since we already have a method (that isn't documented) to get the app's binary version:

async function getAppVersion(seperator = ".") {
  const [{appVersion}, update] = await Promise.all([
    CodePush.getConfiguration(),
    CodePush.getUpdateMetadata()
  ]);

  if (!update) {
    return appVersion;
  }

  const label = update.label.substring(1);
  return `${appVersion}${seperator}${label}`;
};

All 10 comments

The getUpdateMetadata method will only ever return information about a CodePush update, not the binary. Therefore, it will return null if you aren't currently running a CodePush update (or if there isn't a pending update, depending on what UpdateState you specify).

Are you seeing different behavior? Or are you requesting that it returns information about the binary if the user is running the binary-bundled content, as opposed to a CodePush update?

Apologies if I'm misunderstanding you. We haven't heard any issues with the getUpdateMetdata method, so we'll definitely try to figure out how to unblock you here.

Yes, that's what I'm trying to achieve.
Thanks for the quick response.

@avishayil What do you think about us adding a new method called getAppVersion (or something similar) that would return the version of the binary, with the running CodePush label appended to it (if applicable)? This way you could simply call this method and use the return type in all circumstances?

// This would return "1.0.0" if the app is running the binary, and
// "1.0.0.1", "1.0.0.2", etc. if the app was running a CodePush update
const version = await codePush.getAppVersion();

// Same behavior as above, but would concat the binary version and
// CodePush update with a hyphen instead of a period (e.g. "1.0.0-1").
const version = await codePush.getAppVersion("-");

If possible, I'd prefer to keep the behavior of getUpdateMetadata as it is, as opposed to trying to overload it to expose binary information as well.

If the above solution looks interesting to you, you could actually implement it on your end in the meantime (or customize it), since we already have a method (that isn't documented) to get the app's binary version:

async function getAppVersion(seperator = ".") {
  const [{appVersion}, update] = await Promise.all([
    CodePush.getConfiguration(),
    CodePush.getUpdateMetadata()
  ]);

  if (!update) {
    return appVersion;
  }

  const label = update.label.substring(1);
  return `${appVersion}${seperator}${label}`;
};

Yeah, that could work, thanks!

@avishayil Did that proposed solution end up working for you? We'd like to add this capability to the core plugin, and it would be great to get your feedback on how well it worked.

Actually it did. Thanks!
But, I couldn't figure out why you need to await for it, since the local label is already installed on your device, and therefore should be available without expensive queries like that.

This information is actually a part of the native bundle and making Async calls would be the only way this can be accessed. We do not save this information in the JS bundle that is sent back.

Looks like the issue is solved for you, so closing this issue. Please re-open if you have questions

I was looking for that exact code.
Works for me, thank you..!

latest version doesnot export getConfiguration function anymore, "Property 'getConfiguration' does not exist on type 'typeof CodePush'.ts(2339)"

@alzalabany the code still works. Just add a ts-ignore to suppress the warning.

Was this page helpful?
0 / 5 - 0 ratings