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

Add documentation for bundling realm files #532

Open
alazier opened this issue Jul 17, 2016 · 13 comments
Open

Add documentation for bundling realm files #532

alazier opened this issue Jul 17, 2016 · 13 comments
Labels

Comments

@alazier
Copy link
Contributor

alazier commented Jul 17, 2016

No description provided.

@chaodoze
Copy link

chaodoze commented Oct 4, 2016

Any update on this? Where should we store the realm file, and what path should we specify?

@alazier
Copy link
Contributor Author

alazier commented Oct 4, 2016

@chaodoze On iOS you should store the file in the resources directory. You can open files directly from the resources directory in read-only mode, or copy them to the Documents directory first if you need to write to them. There is a utility method Realm.copyBundledRealmFiles() which copies all realm files from the root of the resources directory to the Documents directory if they don't yet exist. Sorry for not updating this yet, we are hard at work trying to get sync built and shipped for react-native.

@chaodoze
Copy link

chaodoze commented Oct 4, 2016

@alazier wow, thanks for the speedy reply. For read-only mode in the iOS resource directory, do we need to specify a path or just the filename e.g. would the following work, if test.realm were in the resource directory?
new Realm({path:'test.realm', readOnly:true})

@alazier
Copy link
Contributor Author

alazier commented Oct 4, 2016

You would need to specify the full path for the resources dir. Relative
paths are relative to the documents directory.
On Tue, Oct 4, 2016 at 12:11 chaodoze notifications@github.com wrote:

@alazier https://github.com/alazier wow, thanks for the speedy reply.
For read-only mode in the iOS resource directory, do we need to specify a
path or just the filename e.g. would the following work, if test.realm were
in the resource directory?
new Realm({path:'test.realm', readOnly:true})


You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
#532 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAPRIKImM-cD9i1sbHjZhJ6uc1ag5Iy1ks5qwqTQgaJpZM4JOSjz
.

@chaodoze
Copy link

chaodoze commented Oct 4, 2016

Thanks @alazier, would you know how I can discover the absolute path of a
file in the resource dir?

On Tue, Oct 4, 2016 at 12:45 PM, Ari Lazier notifications@github.com
wrote:

You would need to specify the full path for the resources dir. Relative
paths are relative to the documents directory.
On Tue, Oct 4, 2016 at 12:11 chaodoze notifications@github.com wrote:

@alazier https://github.com/alazier wow, thanks for the speedy reply.
For read-only mode in the iOS resource directory, do we need to specify a
path or just the filename e.g. would the following work, if test.realm
were
in the resource directory?
new Realm({path:'test.realm', readOnly:true})


You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
#532 (comment),
or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAPRIKImM-
cD9i1sbHjZhJ6uc1ag5Iy1ks5qwqTQgaJpZM4JOSjz>

.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#532 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AADKjRp6OqHigCsEFltvSblRqjFjRs6Vks5qwqy_gaJpZM4JOSjz
.

@alazier
Copy link
Contributor Author

alazier commented Oct 4, 2016

In swift/objective-c you can get this from NSBundle - https://developer.apple.com/reference/foundation/bundle/1410989-path

@chaodoze
Copy link

chaodoze commented Oct 4, 2016

hmm, what about React Native?

On Tue, Oct 4, 2016 at 3:13 PM, Ari Lazier notifications@github.com wrote:

In swift/objective-c you can get this from NSBundle -
https://developer.apple.com/reference/foundation/bundle/1410989-path


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#532 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AADKjQLOce3U0h3uJGQYxpiUZo3lp0u9ks5qws-BgaJpZM4JOSjz
.

@alazier
Copy link
Contributor Author

alazier commented Oct 6, 2016

Doesn't seem like there is an easy way to get this from the RN apis. Might need a native module to expose this, or you can get this info in your Appdelegate. In our test files we use Realm.copyBundledRealmFiles() to copy the files over.

@martsie
Copy link

martsie commented Feb 12, 2017

I just got this working in React Native without having to do a copyBundleRealmFiles.

If you use the library react-native-fs (https://github.com/johanneslumpe/react-native-fs) you can get the bundle path using its MainBundlePath method.

My Realm initialiser looks like:

const parksDb = new Realm({
  path: RNFS.MainBundlePath + '/realm.realm',
  readOnly: true
});

The database filename I baked up using the json importer example was named realm.realm and dragged into the root of the Xcode project directory of my React Native app.

@lfeddern
Copy link

lfeddern commented Mar 19, 2017

@alazier @chaodoze Hi there,
I put a realm file called test2.realm in the assets folder of the android folder.

Then ran:

Realm.copyBundledRealmFiles();

In the constructor of my app componenent. However when I rebuild I get this error:

Realm.copyBundledRealmFiles();

FAILURE: Build failed with an exception.

  • What went wrong:
    Could not list contents of '/Users/user/GitHub/Seneca-Trial-Software/SenecaV2/android/app/src/main/assets/default.realm.note'.

When I open the test2.realm file with the realm browser it opens fine.

Any pointers on what I might be doing wrong??

Thanks v much for your time!

@lfeddern
Copy link

Ok think above error was due to clearing the device data when starting the emulator. Have managed to get there now. Have documented exactly what I did here:
http://stackoverflow.com/a/42888485/5077248
Just in case anyone else struggles.

@andersonaddo
Copy link

andersonaddo commented Feb 9, 2019

@alazier It doesn't look like copyBundleRealmFiles is documented anywhere still. I think it'd be really helpful if this method was fully explained in some documentation.

Also, when you guys (@martsie and @alazier) refer to the iOS resource folder, do you mean the \ios\<appname>.xcodeproj folder in the React Native project directory?

@johannmiller
Copy link

@alazier I have the same question as @andersonaddo. I tried placing my pre-populated default.realm under ios/<appname>.xcodeprog but copyBundledRealmFiles does not find it. Clarification about the "resources" folder would be great.

I've got it working for android (by putting it in android/app/src/main/assets/).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants