Problem with getting Realm instance on Android

Hello,

When I try to get a realm instance the whole app stops, a white screen occurs (I have UI, but it doesn’t render) and after a few seconds “App isn’t responding” (yesterday it didn’t show the "app isn’t responding error, I don’t know why today it started to do that).

It’s not happening always, one way to solve this is to sign out and sign in again user (I’m using login with email and password), but after some time this happens again. One time it happened after I terminated Sync and created it again, but besides that one time, it seems like it happens randomly.

I tried to log user id before getting realm instance and it does it correctly all the time, but for some reason, I need to relog it. It doesn’t execute any code after getting Realm Instance. It doesn’t throw any errors, just logging this over and over every few seconds:

D/REALM_JAVA: HTTP Request = 
    POST https://eu-central-1.aws.realm.mongodb.com/api/client/v2.0/auth/session
    Content-Type: application/json;charset=utf-8
    Accept: application/json
    Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJiYWFzX2RhdGEiOm51bGwsImJhYXNfZGV2aWNlX2lkIjoiNjEyOGQ5NzkxM2E2Yjg5NDI0OGNlZmU1IiwiYmFhc19kb21haW5faWQiOiI2MTIyNDVlZTFiMzEyNTNlNmQzNTY0ZmIiLCJiYWFzX2lkIjoiNjEyOGQ5NzkxM2E2Yjg5NDI0OGNlZmU2IiwiYmFhc19pZGVudGl0eSI6eyJpZCI6IjYxMjI2YTc5MTJmY2QyZTk1YjFlZWFmMCIsInByb3ZpZGVyX3R5cGUiOiJsb2NhbC11c2VycGFzcyIsInByb3ZpZGVyX2lkIjoiNjEyMjUwYmFjOTNiNTI3NmE1NDEzMWQ4In0sImV4cCI6MTYzNTI1MTA2NSwiaWF0IjoxNjMwMDY3MDY1LCJzdGl0Y2hfZGF0YSI6bnVsbCwic3RpdGNoX2RldklkIjoiNjEyOGQ5NzkxM2E2Yjg5NDI0OGNlZmU1Iiwic3RpdGNoX2RvbWFpbklkIjoiNjEyMjQ1ZWUxYjMxMjUzZTZkMzU2NGZiIiwic3RpdGNoX2lkIjoiNjEyOGQ5NzkxM2E2Yjg5NDI0OGNlZmU2Iiwic3RpdGNoX2lkZW50Ijp7ImlkIjoiNjEyMjZhNzkxMmZjZDJlOTViMWVlYWYwIiwicHJvdmlkZXJfdHlwZSI6ImxvY2FsLXVzZXJwYXNzIiwicHJvdmlkZXJfaWQiOiI2MTIyNTBiYWM5M2I1Mjc2YTU0MTMxZDgifSwic3ViIjoiNjEyMjZhZTUxMmZjZDJlOTViMWVmMDZlIiwidHlwIjoicmVmcmVzaCJ9.ljuoLd-FuSMWu_4g4G06X4Zk7vixibqTxdBhMpng0SA[/code]

I attached screenshots and code below.

MainActivity.java

    private App app;
    private Realm realm;
    private User user;

private void initiateRealm() {
        String partitionValue = "bluethread";
        Log.d(TAG, user.getId());

        SyncConfiguration config = new SyncConfiguration.Builder(user, partitionValue).build();

        Realm.getInstanceAsync(config, new Realm.Callback() {
            @Override
            public void onSuccess(Realm realm2) {
                realm = realm2;
                Log.v(TAG, "Successfully opened a realm with reads and writes allowed on the UI thread.");
            }

            @Override
            public void onError(Throwable exception) {
                super.onError(exception);
                Log.d(TAG, "Error initializing Realm: " + exception.getMessage());
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        app = ((MongoDB)getApplication()).app;
        user = app.currentUser();

        if (user != null) {
            initiateRealm();
        } else {
            startActivity(new Intent(context, LoginActivity.class));
        }

        navigation();
    }

MongoDB.java

public class MongoDB extends Application {
    private static final String TAG = "MongoDB";
    private static final String APP_ID = "app_id";

    public App app;

    @Override
    public void onCreate() {
        super.onCreate();

        Realm.init(this);
        app = new App(new AppConfiguration.Builder(APP_ID).defaultSyncErrorHandler(new SyncSession.ErrorHandler() {
            @Override
            public void onError(SyncSession session, AppException error) {
                Log.d(TAG, "MongoDB sync error" + error.getErrorMessage());
            }
        }).build());

        if (BuildConfig.DEBUG)
            RealmLog.setLevel((LogLevel.ALL));

        Log.d(TAG, "Initialized the Realm App configuration.");
    }
}

I’m trying to figure it out for 2 days already, is someone can help me, I would be grateful :slight_smile:

Sorry for the terrible formatting of this post, but I’m not familiar with forum and now I have no idea how to edit it.

This should have been fixed in 10.8.0, which was just released a few hours ago: realm-java/CHANGELOG.md at master · realm/realm-java · GitHub

2 Likes

What great news :smiley: It seems to work right now, thank you so much :slight_smile:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.