简体   繁体   English

调用 .Dispose() 后无法删除领域

[英]Unable To DeleteRealm after .Dispose() Called

Loading in my data加载我的数据

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;

Trying to copy .realm file with same format from a USB Drive尝试从 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
}

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

I've already seen this thread , which is the closest I've found to my issue and a possible solution, although it was for IOS so I'm not sure how much is applicable to my situation.我已经看过这个线程,这是我发现的最接近我的问题和可能的解决方案的线程,尽管它是针对 IOS 的,所以我不确定有多少适用于我的情况。

It's likely that the Realm is still being used by the synchronization thread - eg if you added a whole lot of data to it and it hasn't been uploaded yet.同步线程可能仍在使用该领域 - 例如,如果您向其中添加了大量数据并且尚未上传。 What you can do is either wait for the upload to complete by calling:您可以做的是通过调用等待上传完成:

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

or manually stopping the session (but that would it's possible that not all data has been uploaded):或手动停止会话(但可能并非所有数据都已上传):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM