Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use new Android Architecture without using Lambda Expressions?

I have followed this tutorial, written directly by google.

The problem, that I currently have, is the line userDao.save(response.body()); inside the UserRepository class.

 private void refreshUser(final String userId) {
        executor.execute(() -> {
            // running in a background thread
            // check if user was fetched recently
            boolean userExists = userDao.hasUser(FRESH_TIMEOUT);
            if (!userExists) {
                // refresh the data
                Response response = webservice.getUser(userId).execute();
                // TODO check for error etc.
                // Update the database.The LiveData will automatically refresh so
                // we don't need to do anything else here besides updating the database
                userDao.save(response.body());
            }
        });
    }

When I try to do that in my Android Studio Version, I get a message which says that Lambda expressions are not supported at this language level.

enter image description here

I am aware of the fact that I can upgrade my Android Studio to support Java 8 like that, BUT- is there an other way to do so? I don't want to upgrade to Java 8 just to use Lambda Expressions.

like image 721
romaneso Avatar asked Nov 20 '25 09:11

romaneso


1 Answers

If you don't want to use Java 8 (which you should reconsider), you can manually replace:

executor.execute(() -> {
    // task...
});

with:

executor.execute(new Runnable() {
    @Override
    public void run() {
        // task...
    }
});
like image 168
chornge Avatar answered Nov 22 '25 22:11

chornge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!