Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty DB after application restart #1432

Closed
HellAvalor opened this issue Aug 28, 2015 · 9 comments
Closed

Empty DB after application restart #1432

HellAvalor opened this issue Aug 28, 2015 · 9 comments

Comments

@HellAvalor
Copy link

Hi everyone.
I've ununderstandable problem with storing.

  1. I`m getting instance of realm in application onCreate
    like realm = Realm.getInstance(this);
  2. in Activities getting link to that realm from application reading and writing. Everything going fine and realm has data.
  3. Closing application from processes, start it again and trying to get any data from realm - but it`s empty.

I`m not deleting realm, not clearing it, not clearing cache. So what is it could be?
realm version io.realm:realm-android:0.82.1

@bmunkholm
Copy link
Contributor

Sorry to hear that! Could you possibly share your project? maybe privately if needed to help@realm.io. That would make it super simple to help you. Alternatively try to share all relevant Realm code here.
Thanks

@HellAvalor
Copy link
Author

Sorry but application is under NDA and very big...

but i`ll try

in CustomApplication extend Aplication

    private Realm realmDB;
    public void onCreate() {
        super.onCreate();
        realmDB = Realm.getInstance(this);
    }

    public Realm getRealmDB() {
        return realmDB;
    }

in first activivty

       Realm realm = ((CustomApplication) getApplication()).getRealmDB();
        realm.beginTransaction();
        realm.copyToRealm(theme);
        realm.commitTransaction();

theme is not null CustomizeTheme class

in other place

realm = ((CustomApplication) getApplication()).getRealmDB();
realm.where(CustomizeTheme.class).findFirst()

@cmelchior
Copy link
Contributor

Given the code you have provided it should work, so most likely you are deleting the object in some other piece of the code.

Have you tried putting a test directly into your custom application to check? Something like:

public class MyApplication extends Application {
 private Realm realmDB;
    public void onCreate() {
        super.onCreate();
        realmDB = Realm.getInstance(this);
        CustomizeTheme ct = realm.where(CustomizeTheme.class).findFirst();
        if (ct == null) {
          Log.i("Test", "No theme");
        } else {
          Log.i("Test", "Found theme");
        }
    }

    public Realm getRealmDB() {
        return realmDB;
    }
}

@HellAvalor
Copy link
Author

I'll test it on Monday
On Aug 28, 2015 9:19 PM, "Christian Melchior" notifications@github.com
wrote:

Given the code you have provided it should work, so most likely you are
deleting the object in some other piece of the code.

Have you tried putting a test directly into your custom application to
check? Something like:

public class MyApplication extends Application {
private Realm realmDB;
public void onCreate() {
super.onCreate();
realmDB = Realm.getInstance(this);
CustomizeTheme ct = realm.where(CustomizeTheme.class).findFirst();
if (ct == null) {
Log.i("Test", "No theme");
} else {
Log.i("Test", "Found theme");
}
}

public Realm getRealmDB() {
    return realmDB;
}

}


Reply to this email directly or view it on GitHub
#1432 (comment).

@HellAvalor
Copy link
Author

3152-3152/? I/Test﹕ No theme

So where could I delete it?

@cmelchior
Copy link
Contributor

Hi @HellAvalor

Some places to look

RealmObject.removeFromRealm()
RealmList.remove()
RealmResults.remove()
Realm.clear()
Realm.deleteRealm()

Also if you have some code that is interacting with your apps files. Realm will save the file in data/data/<packagename>/files/default.realm.

@HellAvalor
Copy link
Author

I found problem... I`m clearing files from data/data//files/ directory after init of realm and RealmDB is working from memory but not storing anything to disk because of no file there.

So it was hard to find problem, because that is not directly connected to RealmDB.

it was like

public class MyApplication extends Application {
 private Realm realmDB;
    public void onCreate() {
        super.onCreate();
        realmDB = Realm.getInstance(this);

        String sdcard = getFilesDir().getPath();

        File fileList = new File(sdcard);

        // so we can list all files
        File[] filenames = fileList.listFiles();

        // loop through each file and delete
        for (File tmpf : filenames)
            tmpf.delete();
    }
}

Thanks.

@cmelchior
Copy link
Contributor

Ok, great you found it.

A few notes about your code. getFilesDir() is actually not on the SDCard but a private folder only available to your app. Also there is a getCacheDir() ( http://developer.android.com/reference/android/content/Context.html#getCacheDir() ) if you want to store some temporary files.

@cmelchior
Copy link
Contributor

@HellAvalor I'll close this issue now. Feel free to open another use if you run into any other problems with Realm.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants