Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deliver app with prefilled realm-database

Tags:

realm

I would like to deliver my app with already prefilled data in my realm database. Do I have to simply copy it to the documents directory or is there some other things to do?

like image 871
swalkner Avatar asked Apr 24 '15 05:04

swalkner


People also ask

What is Realm Nosql database how can we implement in Android?

Realm Database is a service which is provided by MongoDb which is used to store data in users device locally. With the help of this data can be stored easily in users' devices and can be accessed easily. We can use this database to store data in the user's device itself.

How do I open a Realm file in iOS?

Open Finder in that folder: open ~/Library/Developer/CoreSimulator/Devices and then create a "saved search" so that you can always find all of your realm files.


1 Answers

Realm's documentation has a section on "Bundling a Realm with an App":

It’s common to seed an app with initial data, making it available to your users immediately on first launch. Here’s how to do this:

  1. First, populate the realm. You should use the same data model as your final, shipping app to create a realm and populate it with the data you wish to bundle with your app. Since realm files are cross-platform, you can use an OS X app (see our JSONImport example) or your iOS app running in the simulator.

  2. In the code where you’re generating this realm file, you should finish by making a compacted copy of the file (see -[RLMRealm writeCopyToPath:error:]). This will reduce the Realm’s file size, making your final app lighter to download for your users.

  3. Drag the new compacted copy of your realm file to your final app’s Xcode Project Navigator.

  4. Go to your app target’s build phases tab in Xcode and add the realm file to the “Copy Bundle Resources” build phase.

  5. At this point, your bundled realm file will be accessible to your app. You can find its path by using [[NSBundle mainBundle] pathForResource:ofType:].

  6. You can either create a read-only realm by calling [RLMRealm realmWithPath:readOnly:error:]. Or, if you’d like to create a writable realm file based on this initial data, you can copy the bundled file to your application’s Documents directory using [[NSFileManager defaultManager] copyItemAtPath:toPath:error:] and then construct your new realm by using [RLMRealm realmWithPath:].

You can refer to our migration sample app for an example of how to use a bundled realm file.

like image 175
jpsim Avatar answered Oct 04 '22 01:10

jpsim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!