Categories
discuss

RxJava pattern for requesting a remote Observable with a temporary cache

The use case is this: I want to temporarily cache the latest emitted expensive Observable response, but after it expires, return to the expensive source Observable and cache it again, etc.

A pretty basic network cache scenario, but I’m really struggling to get it working.

private Observable<String> getContentObservable() {

    // expensive upstream source (API, etc.)
    Observable<String> sourceObservable = getSourceObservable();

    // cache 1 result for 30 seconds, then return to the source
    return sourceObservable
            .replay(1, 30, TimeUnit.SECONDS)
            .autoConnect()
            .switchIfEmpty(sourceObservable);
}

Initial request: goes to source Second request within 30 seconds of source emitting: delivered from cache Third request outside of cache expiry window: nothing. I subscribe to it and I get no data, but it’s not switching to the upstream source Observable.

It looks as if I’m just connecting to my ConnectableObservable from autoConnect() and it’s never completing with empty, so it’s never triggering my switchIfEmpty().

How can I use this combination of replay(1,x,x) and switchIfEmpty()?

Or am I just approaching this wrong from the start?

Answer

So it’s turns out you can use Jake Wharton’s replaying share to cache the last value even after dispose. https://github.com/JakeWharton/RxReplayingShare

Categories
discuss

How to use Property placeholders in .yml file

I am working with Java and spring boot. I was wondering how to add Property placeholders into .yml files. I’ve found some crisp examples but I am not sure where are the Property placeholders are being instantiated in. Is it in system env variables, a file, etc..?

Bootstrap.yml

spring:
  cloud:
    config:
      username: ${my.stored.files.username}
      password: ${my.stored.files.password}
      label: ${spring.cloud.find.label}
      uri: ${spring.cloud.config.uri}
      enabled: false
      failFast: true

User is using Property placeholders, but where did the user declared them? Where is this .yml reading the values from? (same question as above) Is there a document that explains the connection?

This web application will be pushed to cloud foundry using “cf push”, Which will automatically pick manifest.yml file to configure. If possible a cloud foundry example would be great.

Understanding/ Sample Application.properties file

app.name=MyApp
app.description=${app.name} 

User was able to use ${app.name} because it is defined. I am confused on the example above. How and where is the user getting “${my.stored.files.username}. Where is that being defined? I assumed it would be in system.properties or environment variables. Can anyone confirm?

Answer

After intensive research, I was able to find that when I use placeholders in .yml files it reads that values from environment variables. Which was part of my theory in the beginning, but no one has confirmed.

Answer for local environment

spring:
  cloud:
    config:
      username: ${my.stored.files.username}
      password: ${my.stored.files.password}
      label: ${spring.cloud.find.label}
      uri: ${spring.cloud.config.uri}
      enabled: false
      failFast: true

*In environment variables *

Image as example

set key as: my.stored.files.username
set value as: UsernameSample

Then

When you run application, yml will read like so.

    config:
      username: ${my.stored.files.username}
                //gets replaced with UsernameSample

This is the link that solved my problem link

For Cloudfoundry

You would have to create cups or manually add these variables onto the service.

Categories
discuss

RxJava “serialize” method use cases

I want to know cases in which we need to “serialize” observable, its necessary. I mean cases in which “onNext” method will get called concurrently instead of one after another.

Answer

Originally, serialize() was introduced to fix some badly behaving custom Observables from the consumer’s end instead of making the author fix his create()-ion in 1.x. As the enforcing of the Observable protocol became more prominent, there was less need for the operator in practice. It still ended up in v2 just in case and for “nostalgic reasons”. Note that its sibling, Subject.toSerialized() is completely legitimate and is there to serialize access to a Subject in case it is driven from multiple threads.

Categories
discuss

Unable to extract object values in Typescript

I have been trying to convert a JavaScript web form to Typescript, and have been unable to work out how to deal with the following (which works in JavaScript):

let fieldValues = JSON.parse(cookieData);

let keys = Object.keys(fieldValues);

let values = Object.values(fieldValues);

Visual Studio tells me:

Error TS2339 Property ‘values’ does not exist on type ‘ObjectConstructor’.

What can I do?

Answer

The Object.values(..) is not stabilized, so it is unsupported in many browsers (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values)

Use map instead:

let values = Object.keys(fieldValues).map(key => fieldValues[key]);

If you really want to use this function you need to add the "es2017.object" lib on your tsconfig.json. Make sure you are using a polyfill or if your final platform support this feature.

Categories
discuss

Android Espresso doesn’t wait for fragments to load

I’m just getting started with Espresso for testing Android apps, but I’m having some trouble. I have an Activity with a button that replaces a fragment in the usual way:

public void onClick(View v) {
    final FragmentTransaction t = getFragmentManager().beginTransaction();
    t.setCustomAnimations(R.animator.fragment_slide_in_up, 0);
    t.replace(R.id.fragment_container,
        LogInFragment.newInstance(), LogInFragment.TAG)
        .addToBackStack(LogInFragment.TAG);
    t.commit();
}

In my test, I click the button that sends in the new fragment, and then just check that the new fragment is visible.

onView(withId(R.id.login_btn))
    .perform(click());
Thread.sleep(500);
onView(withId(R.id.email_input))
    .check(matches(isDisplayed()));

If I remove the Thread.sleep() from the test, the test fails, even if I remove the setCustomAnimations() from the Activity code.

All animations are turned off on my phone, as per the instructions. My understanding is that Espresso knows about the UI thread status and will wait until everything is ready. But if I do a onView(withId(R.id.widgetOnNewFragment)) it blows up every time. I need to add a Thread.sleep(500) every time I show a new fragment.

Am I missing something? I think I should not have to add dozens of Thread.sleep() to my test code.

Answer

While the Espresso framework ‘waits’ for certain main UI thread activities, animations require the IdlingResource approach (as Honza suggests).

There are good tutorials on this concept, even detailed implementations.

In reality, because animations rarely factor in to functional testing of the UI, most developers disable animations. IMO, this is an anti-pattern because it doesn’t map to the real user experience (most people buy a phone and use it with default settings). This is a decision you’ll have to make yourself. If you want to include animations as part of your functional requirements, there are ways, but it requires you to reimplement and rewrite your tests.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..