简体   繁体   中英

Realm database doesn't Write data

I was trying to write to db using realm but it does writes values. I also added

apply plugin: 'android-apt' apply plugin: 'realm-android'

I'm using it in a Async Task. Below How can I write correctly to realm database

protected Integer doInBackground(Integer[] p1)
{
    Realm.init(acti);
    RealmConfiguration otherConfig = new RealmConfiguration.Builder().directory(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/xMapper/"+fileN)).build();
    Realm.setDefaultConfiguration(otherConfig);
    realm = Realm.getDefaultInstance();

    for (File file:foxReaderLost)
    {   
        final byte[] x=utilz.compress(FileUtils.readFileToString(file));
        final String y=file.getName();

        realm.executeTransaction(new Realm.Transaction(){
                @Override
                public void execute(Realm p1)
                {
                    IconModel icm=p1.createObject(IconModel.class);
                    icm.setName(y);
                    icm.setSource(x);
                }
            });
     }
}

My RealmObject

public class IconModel extends RealmObject {
    private byte[] msource;
    private String mname;

    public void setSource(byte[] lastName)
    {
        this.msource = lastName;
    }

    public void setName(String firstName)
    {
        this.mname = firstName;
    }
}

Init your project, if you don't already have a class extending Application just create one.

class App extends Application {

    @Override
    public void onCreate() {
        Realm.init(acti);
        RealmConfiguration otherConfig = new RealmConfiguration.Builder().directory(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/xMapper/"+fileN)).build();
        Realm.setDefaultConfiguration(otherConfig);
    }
}

Then you just have to call Realm.getDefaultInstance() to get a realm object to query/insert/update/delete your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM