从Xamarin捆绑预先构建的Realm文件

时间:2016-05-28 10:55:30

标签: xamarin realm

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

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

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

3 个答案:

答案 0 :(得分:19)

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

使用Realm中基于Xamarin.Forms的{​​{3}}示例,您可以将填充的数据库作为链接项添加到每个本机项目,并在应用程序启动时将其复制。

实施例

Xamarin.Form解决方案/应用程序的iOS“原生”项目中,如果用户数据库 NOT 存在,请更新FinishedLaunching方法以复制预先填充的数据库(即这是应用程序第一次运行):

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依赖服务是这样做的替代方法,但结果是相同的

由于我们正在将prepopulated.realm复制到应用程序文档目录中的journal.realm,我们可以告诉Realm打开此数据库而不是创建/使用default.realm数据库。

Xamarin.Form期刊项目的JournalEntriesViewModel.cs中,更新代码以打开此journal.realm

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

解决方案中相同的预先填充的数据库链接到不同的“本机”项目:

Journal

答案 1 :(得分:2)

此答案适用于Android,而不是仅适用于iOS的接受答案。

  1. 将prepopulated.realm文件添加到PCL Resources文件夹。通过右键单击并选择“构建操作”&gt;将其标记为嵌入式资源'EmbeddedResource'。

  2. 构建应用。

  3. 从命令行运行dll文件上的 <?php if ( have_posts() ) : ?> <section> <?php echo do_shortcode("[soliloquy slug='categorias']"); ?> <div style="clear:both; height:10px; margin:0 0 20px;"></div> <?php while ( have_posts() ) : the_post(); ?> <article> <header> <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> <time datatime="<?php the_time('F j, Y'); ?>"><i class="fa fa-clock-o"></i> <?php the_time('j F, Y'); ?> &nbsp; | &nbsp; Publicado en: <?php the_category(', ') ?></time> </header> <figure class="category"> <a href="<?php the_permalink(); ?>" rel="bookmark" title="Leer la nota <?php the_title_attribute(); ?>"> <?php // Must be inside a loop. if ( has_post_thumbnail( $_post->ID ) ) { echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . the_title_attribute( array( 'echo' => 0 ) ) . '">'; echo get_the_post_thumbnail( $_post->ID, 'medium' ); echo '</a>'; } else { echo '<img src="http://www.changoonga.com/wp-content/uploads/2016/10/no-foto.jpg" title="no hay foto" />'; } ?> </a> </figure> <div class="cat-excerpt"><?php the_excerpt(); ?></div> <div style="clear:both; height:0px; margin:0"></div> </article> <?php endwhile; ?> <div class="pagination"> <span><?php next_posts_link('« Notas anteriores'); ?></span> <span><?php previous_posts_link('Notas recientes »'); ?></span> </div> </section> <?php else : ?> <p><?php _e('Ups!, no hay entradas.'); ?></p> <h2>Puedes ver lo más reciente o intentar con otra búsqueda</h2> <?php echo do_shortcode("[display-posts posts_per_page='10' image_size='medium' include_excerpt='true']"); ?> <?php endif; ?> ,在monodic --manifest YourApp.dll中找到Debug。找到YourApp/Droid/bin/Debug行并复制资源字符串。

  4. 编辑您的MainActivity.cs并在prepopulated.realm行后面添加以下代码。

  5. enter image description here

答案 2 :(得分:0)

对于想要在本机Xamarin.Android上捆绑域文件的任何人,您可以使用以下代码。捆绑的域文件可以与最终的域文件同名。只需将realm文件复制到Assets目录,并确保将构建操作设置为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);
    }
}