【问题标题】:Unable To DeleteRealm after .Dispose() Called调用 .Dispose() 后无法删除领域
【发布时间】:2020-03-16 16:29:58
【问题描述】:

加载我的数据

Uri.TryCreate(realmPath, UriKind.Relative, out var outPathUri);
RealmConfigurationBase configuration = new FullSyncConfiguration(outPathUri, _currentUser, _realmFile)
{
    ObjectClasses = typesInSelectedRealm,
    SchemaVersion = 1
};

_realmInstance = Realm.GetInstance(configuration);

if(_realmInstance != null) _realmInstance.RealmChanged += LoadDataOnChange;

尝试从 USB 驱动器复制相同格式的 .realm 文件

if(_realmInstance != null) 
{
    _realmInstance.RealmChanged -= LoadDataOnChange; // figure it's good to clear all external references to Realm object
    _realmInstance.Dispose();
    GC.Collect();
    Thread.Sleep(1000); // both of these are me trying to just make darn sure that everything is cleaned up and has had enough time to do so
    Realm.DeleteRealm(_realmInstance.Config); // error
}

抛出的错误:The process cannot access the file '...\test.realm' because it is being used by another process.

我已经看到this thread,这是我发现的最接近我的问题和可能的解决方案,虽然它是针对 IOS 的,所以我不确定有多少适用于我的情况。

【问题讨论】:

  • 适用。如果您连接到 Realm - 您已连接并且无法删除。您必须任何初始连接之前将其删除。

标签: c# .net-core realm


【解决方案1】:

同步线程可能仍在使用 Realm - 例如如果您向其中添加了大量数据并且尚未上传。您可以做的是通过调用等待上传完成:

await _realmInstance.GetSession().WaitForUploadAsync();
_realmInstance.Dispose();
// wait a second
// delete

或手动停止会话(但有可能并非所有数据都已上传):

_realmInstance.GetSession().Stop();
_realmInstance.Dispose();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多