从 Xamarin 捆绑预构建的 Realm 文件 [英] Bundle prebuilt Realm files from Xamarin

查看:26
本文介绍了从 Xamarin 捆绑预构建的 Realm 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些 SO 帖子,详细介绍了如何将预构建的 Realm 文件与 iOS (Obj-c/swift) 和 Android (Java) 捆绑在一起,但我无法从 PCL 或共享中找到有关与 Xamarin 捆绑的任何信息项目;这可能吗?

由于文件在每个平台上的分布方式的细微差别,我相信它需要一个每个项目的 *.realm 文件(例如,在编译时从单个源复制),但这是一个很小的代价从两个平台上的共享代码访问预构建的数据.

我的目标是在第一次启动应用程序时避免初始下载过程.

解决方案

您可以将预填充的 .realm 数据库作为资源添加到您的应用程序 iOS 包中 (BundleResource> 构建操作)并作为原始资产到您的 Android 资产目录(AndroidAsset 构建操作).

使用基于 Xamarin.Forms

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.

解决方案

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:

这篇关于从 Xamarin 捆绑预构建的 Realm 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆