捆绑来自Xamarin的预构建Realm文件 [英] Bundle prebuilt Realm files from Xamarin
问题描述
我看过一些SO帖子,详细介绍了如何将预构建的Realm文件与iOS(Obj-c/swift)和Android(Java)捆绑在一起,但是我找不到从PCL或共享的Xamarin捆绑的任何信息.项目;这可能吗?
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?
我认为,由于每个平台中文件分发方式的细微差别,它需要每个项目的* .realm文件(例如,在编译时从一个源复制),但这要付出很小的代价可以从两个平台上的共享代码访问预建数据.
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.
推荐答案
您可以将预先填充的.realm
数据库作为资源(BundleResource
Build Action)和原始资产添加到应用程序iOS捆绑包中. Android Asset目录(AndroidAsset
生成操作).
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).
使用基于Xamarin.Forms
的 Journal 来自Realm的示例,您可以将填充的数据库作为链接项添加到每个本机项目,并在应用程序启动时将其复制.
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.
在iOS中,本地" Xamarin.Form
解决方案/应用程序的项目,如果用户数据库不存在(例如,这是应用程序第一次运行),请更新FinishedLaunching
方法以复制预先填充的数据库:/p>
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);
}
注意:相同的技术可用于您的其他本地"用户.项目
注意:定制编写的Xamarin.Forms
依赖项服务可以替代这种方式,但是结果是相同的
Note: A custom-written Xamarin.Forms
dependency service is an alternative to doing it this way but the results are the same
由于我们将prepopulated.realm
复制到应用程序文档目录中的journal.realm
,因此我们可以告诉Realm打开该数据库,而不是创建/使用default.realm
数据库.
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.
在Xamarin.Form
Journal项目的JournalEntriesViewModel.cs
中,更新代码以打开此journal.realm
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屋!