Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

android - Realm transaction using from different thread

I have writeToRealm method that I use very often. And I need to use it from different threads and cut boilerplate code. What is the better way for this task?

private boolean writeToRealm(String user, String id) {

        SubscriberObject subscriber = new SubscriberObject();

        if(mRealm.where(SubscriberObject.class)
                .equalTo(SubscriberObject.ID,id).findAll().isEmpty()
                &&mRealm.where(SubscriberObject.class)
               .equalTo(SubscriberObject.USERNAME,user).findAll().isEmpty()) {
            subscriber.setId(id);
            subscriber.setUsername(user);
            mRealm.beginTransaction();
            mRealm.insert(subscriber);
            mRealm.commitTransaction();
            return true;
        }
        return  false;

}

I am planning to use construction below (or something like this) but I can't create a correct construction:

  public static Boolean writeToRealm(final String user,final String id){
        Realm mRealm;
        return Flowable.using(
                mRealm = Realm.getDefaultInstance(),
                new Function<Realm, Boolean>() {
                    @Override
                    public Boolean apply(@NonNull Realm realm) throws Exception {
                        SubscriberObject subscriber = new SubscriberObject();
                        if(realm.where(SubscriberObject.class)
                                .equalTo(SubscriberObject.ID,id).findAll().isEmpty()
                                &&realm.where(SubscriberObject.class)
                                .equalTo(SubscriberObject.USERNAME,user).findAll().isEmpty()) {
                            subscriber.setId(id);
                            subscriber.setUsername(user);
                            realm.beginTransaction();
                            realm.insert(subscriber);
                            realm.commitTransaction();
                            return true;
                        }
                        return false;
                    }
                },
                mRealm.close()).subscribeOn(Schedulers.io());
    }

Or may be I need to create a thread class with looper for this task?

How to better integrate this method and similar methods into a clean architecture?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you're just looking for

private boolean writeToRealm(String user, String id) {
    try(Realm realm = Realm.getDefaultInstance()) {           
        if(realm.where(SubscriberObject.class).equalTo(SubscriberObject.ID,id).count() <= 0L 
              && realm.where(SubscriberObject.class).equalTo(SubscriberObject.USERNAME,user).count() <= 0L) {
            final SubscriberObject subscriber = new SubscriberObject();
            subscriber.setId(id);
            subscriber.setUsername(user);
            realm.executeTransaction(r -> r.insert(subscriber));
            return true;
        }
    }
    return false;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...