Search code examples
androidrealmandroid-looper

Realm and Looper shenanigans


I have troubles with Loopers and Realm.

I have an Activity that instanciate its Presenter in onCreate(), and then call one of its public method initFirstLaunch() :

RealmChangeListener<CourseDetailed> listener = new RealmChangeListener<CourseDetailed>() {
    @Override
    public void onChange(CourseDetailed element) {
        Log.i("renaud", "courseDetailed.addChangeListener onChange");
        computeTableOfContent(element);
        Log.i("renaud", "1");
        playerViewContract.initWithCourseDetails(element);
        Log.i("renaud", "2");
    }
};

public void initFirstLaunch() {

    courseDetailed = realm.where(CourseDetailed.class).contains("_id", courseId).findFirst();

    if (courseDetailed == null) {
        courseDetailed = realm.createObject(CourseDetailed.class, courseId);
    }

    courseDetailed.addChangeListener(listener);

    Api.getInstance().backend.getCourse(courseId).enqueue(new CustomRetrofitCallBack<CourseDetailed>(playerViewContract) {
        @Override
        public void onResponseReceived(final CourseDetailed response) {

            Realm.getDefaultInstance().executeTransactionAsync(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    realm.copyToRealmOrUpdate(response);
                }
            });
        }
    });
}

note that playerViewContract is my activity in this context.

My problem is that onChange() is sometimes called, and when it does it blocks my UI Thread (and eventualy provoques a OutOfMemoryError). My guess was that I was not in a looper thread, but when I call Looper.prepare() anywhere it crashes saying that I'm already in one.

What's happening ?

Thanks


Edit : Adding initWithCourseDetailed code

@Override
public void initWithCourseDetails(final CourseDetailed detailed) {

    Log.i("renaud", "initWithCourseDetails");

    mDrawer.post(new Runnable() {
        @Override
        public void run() {

            String title = detailed.getName();
            String subtitle = detailed.getCompany().getName();

            if (detailed.getThumbnail() != null) {

                String picUrl = AppConstants.SERVER_URL + "/api/" + detailed.getThumbnail();

                final LinearLayout l = (LinearLayout) mNavigationView.findViewById(R.id.layout);
                Picasso.with(ModulePlayerActivity.this)
                        .load(picUrl)
                        .config(Bitmap.Config.RGB_565)
                        .into(new Target() {
                            @Override
                            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                                l.setBackground(new BitmapDrawable(getResources(), bitmap));
                            }

                            @Override
                            public void onBitmapFailed(Drawable errorDrawable) {
                            }

                            @Override
                            public void onPrepareLoad(Drawable placeHolderDrawable) {

                            }
                        });
            }

            TextView nameTv = (TextView) mNavigationView.findViewById(R.id.drawer_header_name);
            nameTv.setText(title);

            TextView jobTv = (TextView) mNavigationView.findViewById(R.id.drawer_header_job);
            jobTv.setText(subtitle);

            supportInvalidateOptionsMenu();

        }
    });
}

Edit : correction

Api.getInstance().backend.getCourse(courseId).enqueue(new CustomRetrofitCallBack<CourseDetailed>(playerViewContract) {
            @Override
            public void onResponseReceived(final CourseDetailed response) {

                //TEST
                Realm realm = null;
                try {
                    realm = Realm.getDefaultInstance();
                    realm.executeTransactionAsync(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {
                            realm.copyToRealmOrUpdate(response);
                        }
                    });
                } finally {
                    if (realm != null) {
                        realm.close();
                    }
                    realm = null;
                }

            }
        });

Solution

  • The UI thread is a Looper thread (it has the looper Looper.getMainLooper()), which means it has autoupdate. Auto-update however means that the ChangeListener you add to your RealmObject will be called whenever the underlying table is modified, and not just once.

    (And I should also note that RealmChangeListeners are retained with weak reference by Realm's Context and so is the RealmObject, so to keep the autoupdate and the change listener intact even through GC, it has to be kept as a field reference.

    Although it's also worth noting that findFirst() can return null if the element is not found by ID, otherwise returns the element immediately.)

    So it seems to me that whatever playerViewContract.initWithCourseDetails(element); does, it does it again and again and eventually crashes.

    I kinda hope I answered your question?