导入预填充的Realm数据库

编程入门 行业动态 更新时间:2024-10-25 06:24:39
本文介绍了导入预填充的Realm数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经预填充了领域数据库,所以我具有default.realm.我想在另一个应用程序中使用它,因此将其放在我的\ res \ raw文件夹中.我读到它应该像调用Realm.getInstance()一样容易.我有这样的代码:

I have prepopulated realm database, so I have default.realm. I want to use it in another app so I put it in my \res\raw folder. I read that it should be as easy as calling Realm.getInstance(). I have code like this:

copyBundledRealmFile(this.getResources().openRawResource(R.raw.default0), "default.realm"); realm = Realm.getInstance(this); private String copyBundledRealmFile(InputStream inputStream, String outFileName) { try { File file = new File(this.getFilesDir(), outFileName); FileOutputStream outputStream = new FileOutputStream(file); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buf)) > 0) { outputStream.write(buf, 0, bytesRead); } outputStream.close(); return file.getAbsolutePath(); } catch (IOException e) { e.printStackTrace(); } return null; }

但是它不起作用.我可以在我的应用程序包中看到default.realm,因此我认为执行了copyBundledRealmFile,但是随后发生io.realm.exceptions.RealmMigrationNeededException.我不知道是否可以跳过迁移部分吗?因为我认为我实际上并不迁移,所以请使用预填充的数据库.而且编写迁移"部分不会像调用Realm.getInstance()那样容易.

But it doesn't work. I can see a default.realm in my app package, so I think copyBundledRealmFile was executed, but then io.realm.exceptions.RealmMigrationNeededException occurs. I don't know if there is a way to skip migration part? Because I think I don't actually migrate, just use prepopulated databse. And writing Migration part won't be as easy as calling Realm.getInstance().

推荐答案

我只是遇到了同样的问题.但是问题出在我的模型定义中.我是手工制作的.

I just faced with the same problem. But the issue was in my model definition. I made it manually.

尝试在Realm Browser中打开数据库并保存Java的Model Definition.我做到了,它解决了这个问题.

Try to open the database in Realm Browser and Save Model Definition for Java. I did it and it fixed the issue.

更多推荐

导入预填充的Realm数据库

本文发布于:2023-08-05 20:55:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1308003.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   Realm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!