在应用程序运行时替换Realm文件 [英] Replace Realm file while app is running

查看:106
本文介绍了在应用程序运行时替换Realm文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要实现备份/恢复功能,我将删除现有的Realm数据库文件,并将其替换为具有相同名称的新数据库文件。但是,在应用程序仍在运行的情况下,它不会看到新数据库文件的内容。如果我退出并重新启动应用程序,它会看到新数据库文件的内容。是否有任何方法可以让应用程序看到新内容而无需重新启动它?

解决方案

就像删除Realm文件一样磁盘,如果你的应用程序当前没有打开Realm文件,那么替换磁盘上的Realm文件是唯一安全的。



来自Realm的文档删除领域文件


因为Realm避免将数据复制到内存中,除非绝对需要,所以Realm管理的所有对象都包含对磁盘上文件的引用,并且必须先释放它才能安全删除文件。这包括从Realm读取(或添加到Realm)的所有对象,所有 List Results ThreadSafeReference 对象,以及 Realm 本身。



实际上,这意味着删除Realm文件应该在应用程序启动之前在打开Realm之前完成,或者在仅在显式自动释放池中打开Realm之后完成,这样可以确保所有Realm对象都已被解除分配。


原因是Realm维护一个打开文件的内存缓存,因此尝试打开已经打开的文件会导致对该文件的引用已经打开的文件被返回。此打开文件将继续引用磁盘上的原始文件,即使它已被替换。确保已清除对Realm访问器对象的所有引用意味着Realm将不会返回现有的打开文件,而是从磁盘打开该文件。



In换句话说,您必须确保没有引用Realm的访问者对象( Realm 结果 ThreadSafeReference 对象实例)当您尝试替换Realm文件时。您还必须确保您所做的任何引用已被解除分配(即,它们不会在调度队列的默认自动释放池中延迟)。



可能更容易管理的替代方法是在尝试重新打开还原的文件时使用不同的路径。由于您正在访问磁盘上的其他路径,因此您可以保证打开新文件。您仍然需要确保没有引用Realm的访问器对象,否则您将获得新旧数据的奇怪组合,但是确保访问者对象被解除分配并不是那么重要。 / p>

To implement the backup / restore feature, I am deleting the existing Realm database file and replacing it with the new database file with the same name. However, with the app still running it does not see the contents of the new database file. If I quit and relaunch the app, it does see the contents of the new database file. Is there any way to make the app see the new contents without having to relaunch it?

解决方案

Much like deleting a Realm file from disk, it's only safe to replace a Realm file on disk if your application does not currently have the Realm file open.

From Realm's documentation on Deleting Realm files:

Because Realm avoids copying data into memory except when absolutely required, all objects managed by a Realm contain references to the file on disk, and must be deallocated before the file can be safely deleted. This includes all objects read from (or added to) the Realm, all List, Results, and ThreadSafeReference objects, and the Realm itself.

In practice, this means that deleting a Realm file should be done either on application startup before you have opened the Realm, or after only opening the Realm within an explicit autorelease pool, which ensures that all of the Realm objects will have been deallocated.

The reason for this is that Realm maintains an in-memory cache of open files, so attempting to open a file that's already open will result in a reference to the already-open file being returned. This open file will continue to refer to the original file on disk, even if it has since been replaced. Ensuring that all references to Realm accessor objects have been cleaned up means that Realm will not have an existing open file to return, and will instead open the file from disk.

In other words, you must make sure you have no references to Realm's accessor objects (Realm, Results, ThreadSafeReference or Object instances) at the point when you attempt to replace the Realm file. You must also make sure that any references you did have have since been deallocated (i.e., they're not lingering in a dispatch queue's default autorelease pool).

An alternative approach that may be easier to manage is to use a different path when you attempt to reopen the restored file. Since you're accessing a different path on disk you'll be guaranteed to open the new file. You'll still need to make sure you have no references to Realm's accessor objects since otherwise you'll get a weird mix of old and new data, but it won't be as critical that you ensure that the accessor objects were deallocated.

这篇关于在应用程序运行时替换Realm文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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