Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bundle prebuilt Realm files from Xamarin

Tags:

xamarin

realm

I've seen a few SO posts detailing how you bundle prebuilt Realm files with iOS (Obj-c/swift) and Android (Java), but I can't find any information on bundling with Xamarin from a PCL or shared project; is this possible?

I believe it would require a per-project *.realm file (e.g. copied from a single source at compile time) due to the nuances of how files are distributed in each platform, but that's a small price to pay to be able to access prebuilt data from shared code on both platforms.

My objective is to avoid an initial download process when launching the App for the first time.

like image 584
kez Avatar asked May 28 '16 10:05

kez


3 Answers

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:

enter image description here

like image 159
SushiHangover Avatar answered Nov 18 '22 10:11

SushiHangover


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.

enter image description here

like image 36
E.Freitas Avatar answered Nov 18 '22 11:11

E.Freitas


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);
    }
}
like image 39
Mark Silver Avatar answered Nov 18 '22 09:11

Mark Silver