question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How get value from executeTransaction (inner class)?

See original GitHub issue

My code that insert/update incoming profile to Realm:

    public Profile saveProfile(final String incomingProfileJson) throws Exception {
        Realm realm = Realm.getDefaultInstance();
        try {
            final Profile incomingProfile = JsonUtil.gson.fromJson(incomingProfileJson, Profile.class);
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    Profile findProfile = realm.where(Profile.class).equalTo(Profile.ID, 123).findFirst();
                    if (findProfile != null) {
                        if (incomingProfile.getName() != null) {
                            findProfile.setName(incomingProfile.getName());
                        }
                        if (incomingProfile.getDiscount() != null) {
                           findProfile.setDiscount(realm.copyToRealm(incomingProfile.getDiscount()));
                        }
                    } else { // new oraanization
                        realm.copyToRealmOrUpdate(incomingProfile);
                    }
                }// execute
            });

            // need return after success updated in Realm
            return <<<<how_get_actual_upated_profile>>>>;
        } finally {
            realm.close();

        }
    }

In my code I insert new profile or update exist profile. OK. It’s work fine. But as you can see I must return profile (how_get_actual_upated_profile) that was success inserted/updated to Realm. So I need some method that call IMMEDIATELY AFTER success update profile to Realm. How I can do this. Maybe has some listener?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Zhuindencommented, Sep 24, 2017

Well it’ll do.

0reactions
alexei-28commented, Sep 24, 2017

Here my solution. I user method copyFromRealm. So as result I don’t worry about realm.close.

  public Profile saveProfile(final String incomingProfileJson) throws Exception {
       Realm realm = Realm.getDefaultInstance();
       try {
           final Profile[] resultProfile = new Profile[1];
           final Profile incomingProfile = JsonUtil.gson.fromJson(incomingProfileJson, Profile.class);
           realm.executeTransaction(new Realm.Transaction() {
               @Override
               public void execute(Realm realm) {
                   Profile findProfile = realm.where(Profile.class).equalTo(Profile.ID, 123).findFirst();
                   if (findProfile != null) {
                       if (incomingProfile.getName() != null) {
                           findProfile.setName(incomingProfile.getName());
                       }
                       if (incomingProfile.getDiscount() != null) {
                           findProfile.setDiscount(realm.copyToRealm(incomingProfile.getDiscount()));
                       }
                       // detached(unmanaged) in-memory copy of findProfile
                       resultProfile[0] = realm.copyFromRealm(findProfile);
                   } else { // new profile
                       Profile updatedProfile = realm.copyToRealmOrUpdate(incomingProfile);
                       // detached(unmanaged) in-memory copy of findProfile
                       resultProfile[0] = realm.copyFromRealm(updatedProfile);
                   }
               }// execute
           });
           // don't worry about realm.close because I use detached in-memory copy of Profile
           return resultProfile[0];
       } finally {
           realm.close();
       }
}

And now I return correct updated Profile.

Is this a good solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get value of variable from inner class - java - Stack Overflow
First, your method public ArrayList<Message> getLastTenMessageCallBack(...) has synchronous signature, but implementation is asynchronous.
Read more >
Realm (Realm 4.1.1) - MongoDB
The Realm class is the storage and transactional manager of your object persistent store. ... Objects within a Realm can be queried and...
Read more >
io.realm.Realm.executeTransaction java code examples
@Override public void onComplete(Result<List<TradeTag>> result,Realm realmDefault) { if (!resultHandler(result)){ return; } initList(result.
Read more >
Java - Inner classes - Tutorialspoint
Java - Inner classes, This Java tutorial covers basic to advanced concepts ... to access the private members) call the getValue() method of...
Read more >
Nested Classes - Learning the Java Language
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found