Rich-text Reply

Android - when to use startOptimizelyAsync()

yuval 05-21-15
Accepted Solution

Android - when to use startOptimizelyAsync()

The Android SDK for Optimizely includes the method 

Optimizely.startOptimizelyAsnyc()

However, the only documentation for it exists below:

http://developers.optimizely.com/android/help/reference/com/optimizely/Optimizely.html#startOptimize... android.app.Application)

 

I can't find any other documentation or mention of it. I would like to use this method instead of the regular `startOptimizely` recommended in the quickstart, because I'd like the web request to occur asynchronously instead of relying on the 2.5-second timeout. Are there any risks or downsides to using the async method?

 

Also, I am not interested in any callbacks. Is it ok to pass in null for the callback listener argument? That's what I've done so far, and everything appears to be working smoothly. But please let me know if this may cause any surprises down the line.

 

Thank you!

Level 2

Re: Android - when to use startOptimizelyAsync()

Hi there!

Thanks for the question. Null is a perfectly acceptable value for the callback, we're very careful about handling null inputs on our APIs.

The downside to using the Async start (especially without a listener) is that you may evaluate live variables or code blocks before Optimizely has started, which means that those values will be locked to to the default value, preventing the experiment from starting. I would recommend using a callback and listening for the `onOptimizelyStarted()` event.
Optimizely
yuval 05-28-15
 

Re: Android - when to use startOptimizelyAsync()

Thank you for the response Josiah!

Follow-up question: so what should I do in the onOptimizelyStarted() callback? From your example, I understood that that would be a good time to reload live variables (and code blocks) so I am simply doing something along the lines of:

public void onOptimizelyStarted() {
// myLiveVariable is static
myLiveVariable = Optimizely.booleanVariable("myLiveVariable", false);
}

Does this take care of the issues you mentioned regarding live variables becoming locked to the default value?

Thank you!
Level 2
yuval 05-28-15
 

Re: Android - when to use startOptimizelyAsync()

A clarification on the above: In order for it to work, is it ok if myLiveVariable has already been loaded from Optimizely? Or do I need to set it to null and only load it for the first time once the callback occurs?

Thanks again!
Level 2

Re: Android - when to use startOptimizelyAsync()

You only need to declare/initialize the live variable once:

public static final LiveVariable<Boolean> myLiveVariable = Optimizely.booleanVariable("myLiveVariable", false);

Then, in your callback:

public void onOptimizelyStarted() {
myLiveVariable.get(); // Do something with this value
}

a call to myLiveVariable.get() will return the value according to the experiment.

Any calls to myLiveVariable.get() before startOptimizely has finished will return the default value.

I hope that this helps!
Optimizely
yuval 05-29-15
 

Re: Android - when to use startOptimizelyAsync()

Cool, thank you for the reply. That did indeed help.

Just a suggestion, but maybe you guys should expand your docs and/or add examples like this for using startOptimizelyAsync(). I imagine I'm not the only guy who doesn't want to wait for the Optimizely web request to return synchronously before starting the app...
Level 2