1

I am trying to put a preloaded Realm database in my app.

What I am doing:

  1. I created a simple app that loads data from JSON to the Realm database (default1.realm).

  2. I have my actual app, where I am trying to put the default.realm file created by the loader in the res/raw folder and using the Migration Examples copyBundledRealmFile() function to copy default.realm to getFilesDir().

  3. After doing all this, when I try to do Realm.getInstance() then an empty Realm is returned.

What am I doing wrong?

String path3 = copyBundledRealmFile(this.getResources().openRawResource(R.raw.default1), "default.realm");

Realm r1 = Realm.getInstance(this);

r1 should point to the file I copied, right?

UPDATE: I found why I could not access the database:

03-27 10:27:50.965  17750-17750/? A/art﹕ art/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: 
input is not valid Modified UTF-8: illegal continuation byte 0x2e
03-27 10:27:50.965  17750-17750/? A/art﹕ art/runtime/check_jni.cc:65]     string: 'Unspecified exception. 
Failure when converting long string to UTF-16 error_code = 2; retcode = 0; StringData.size = 40487; StringData.data = etc..

03-27 10:27:50.965  17750-17750/? A/art﹕ art/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: 
input is not valid Modified UTF-8: illegal continuation byte 0x2e
03-27 10:27:50.965  17750-17750/? A/art﹕ art/runtime/check_jni.cc:65]     string: 'Unspecified exception. 
Failure when converting long string to UTF-16 error_code = 2; retcode = 0; StringData.size = 40487; StringData.data =

My question would be now why is this error happening?

Michael Alan Huff
  • 3,462
  • 3
  • 28
  • 44
user1915767
  • 111
  • 1
  • 7

1 Answers1

0

You say that your file is named default.realm and yet you refer to it as default1 in the snippet above. Does it work if you try with default?

geisshirt
  • 2,457
  • 1
  • 17
  • 20
  • I just corrected my post, sorry for that. I made some progress, I removed the characters that cause the above mentioned error, but another came up: I am debugging Realm source code and when doing initializeRealm(realm) (line 529 in Realm.java) I can see the data what is in the file, meanwhile Realm is checking the tables and the fieldnames. But after that the returned Realm object has no tables, nothing. – user1915767 Mar 27 '15 at 11:18
  • It looks like your Realm file is corrupt. Is the file compressed while packaging, and you are opening a compressed Realm file? A compressed Realm file will look like a corrupt file. – geisshirt Mar 27 '15 at 12:27
  • There is no compression. – user1915767 Mar 27 '15 at 12:51
  • How can I check if the file is corrupt after packaging? Is there a way? – user1915767 Mar 27 '15 at 12:52
  • Opening a Realm file is probably the best thing to do. As the log show above indicates that you have a string of length 40487, my best guess is that something went wrong when packaging your app. – geisshirt Mar 27 '15 at 13:32
  • If you are using ProGuard, it might compress the Realm file: http://stackoverflow.com/questions/21440572/proguard-breaking-audio-file-in-assets-or-raw – geisshirt Mar 27 '15 at 13:32
  • I am not using ProGuard either (at the moment) so that shouldn't be a problem either. I opened the realm file in a Realm Browser and I could see the content, so there should be also everything fine. Let me prepare a sample code and put it on github, so we can talk about actual code and not just theory. – user1915767 Mar 30 '15 at 06:30
  • I created the sample project here: https://github.com/gergokajtar/RealmLoader If you can take a look at it, I'd appreciate it. – user1915767 Mar 30 '15 at 12:42
  • I have cloned your sample project. First I ran the database loader app and copied the `default.realm` over and opened with the Realm browser - no problem. Then I built then app and it ran fine too. Opening the Realm files with the Realm browser worked fine. So I haven't been able to reproduce your issue. – geisshirt Mar 31 '15 at 15:24
  • By the way, I was using the emulator and not a real device. Can you tell me how you ran your apps when they failed? – geisshirt Mar 31 '15 at 15:25
  • Hi! I think I was confused when debugging. In the debugger when I was checking out the Realm object I could see 0 tables, so I thought that it cannot read the file. Sorry for the misunderstanding and thanks for the follow up! – user1915767 Mar 31 '15 at 15:51