Bundle prebuilt Realm files from Xamarin

℡╲_俬逩灬. 提交于 2019-11-30 04:02:47

You can add your pre-populated .realm database to your application iOS bundle as a resource (BundleResource Build Action) and as a raw asset to your Android Asset directory (AndroidAsset Build Action).

Using the Xamarin.Forms-based Journal example from Realm you can add your populated database as a linked item to each native project and copy it at application startup.

Example:

In the iOS "native" project of your Xamarin.Form solution/application, update the FinishedLaunching method to copy your pre-populated database if the users database does NOT exist (i.e. this is the first time the application runs):

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    var prepopulated = "prepopulated.realm";
    var realmDB = "journal.realm";
    var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    if (!File.Exists(Path.Combine(documentsPath, realmDB)))
    {
        File.Copy(prepopulated, Path.Combine(documentsPath, realmDB));
    }
    global::Xamarin.Forms.Forms.Init();
    LoadApplication(new App());
    return base.FinishedLaunching(app, options);
}

Note: The same technique can be used in your other "native" projects

Note: A custom-written Xamarin.Forms dependency service is an alternative to doing it this way but the results are the same

Since we are copying the prepopulated.realm to journal.realm within the application document directory, we can tell Realm to open this database instead of creating/using a default.realm one.

In the Xamarin.Form Journal project's JournalEntriesViewModel.cs, update the code to open this journal.realm

public JournalEntriesViewModel()
{
    _realm = Realm.GetInstance("journal.realm");
    Entries = _realm.All<JournalEntry>();
    AddEntryCommand = new Command(AddEntry);
    DeleteEntryCommand = new Command<JournalEntry>(DeleteEntry);
}

Same pre-populated database in the solution linked to different "native" projects:

This answer works for Android, as opposed to the accepted answer which works only for iOS.

  1. Add your prepopulated.realm file to the PCL Resources folder. Mark it as an embedded resource by right clicking and selecting 'Build Action' > 'EmbeddedResource'.

  2. Build the app.

  3. From the command line run the monodic --manifest YourApp.dll on your dll file, which for Debug is found in YourApp/Droid/bin/Debug. Find the line with prepopulated.realm and copy the resource string.

  4. Edit your MainActivity.cs and add the following code right after the base.OnCreate(savedInstanceState); line.

For anyone wanting to bundle a realm file on native Xamarin.Android you can use the following code. The bundled realm file can be the same name as the final realm file. Just copy the realm file to the Assets directory and make sure the build action is set as AndroidAsset.

var realmDB = "somerealm.realm";
var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var filePath = Path.Combine(documentsPath, realmDB);
if (!File.Exists(filePath))
{
    using (var asset = Assets.Open(realmDB))
    using (var destination = File.OpenWrite(filePath))
    {
        asset.CopyTo(destination);
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!