Bundle prebuilt Realm files from Xamarin -
i've seen few posts detailing how bundle prebuilt realm files ios (obj-c/swift) , android (java), can't find information on bundling xamarin pcl or shared project; possible?
i believe require per-project *.realm file (e.g. copied single source @ compile time) due nuances of how files distributed in each platform, that's small price pay able access prebuilt data shared code on both platforms.
my objective avoid initial download process when launching app first time.
you can add pre-populated .realm
database application ios bundle resource (bundleresource
build action) , raw asset android asset directory (androidasset
build action).
using xamarin.forms
-based journal example realm can add populated database linked item each native project , copy @ application startup.
example:
in ios "native" project of xamarin.form
solution/application, update finishedlaunching
method copy pre-populated database if users database not exist (i.e. first time 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: same technique can used in other "native" projects
note: custom-written xamarin.forms
dependency service alternative doing way results same
since copying prepopulated.realm
journal.realm
within application document directory, can tell realm open database instead of creating/using default.realm
one.
in xamarin.form
journal project's journalentriesviewmodel.cs
, update code open 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 solution linked different "native" projects:
Comments
Post a Comment