gpt4 book ai didi

xamarin - 获取异常 : "Already in a write transaction" in Realm for Xamarin iOS

转载 作者:行者123 更新时间:2023-12-02 03:11:51 24 4
gpt4 key购买 nike

我在我的 Xamarin 应用程序中使用 Realm,并且在生产中遇到了这个异常。我从未见过它自己运行该应用程序,而且它每天只发生几次。完整的堆栈跟踪是:

System.AggregateException', reason: 'System.AggregateException: 
A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.
---> Realms.RealmInvalidTransactionException: The Realm is already in a write transaction at Realms.NativeException.ThrowIfNecessary (System.Func`2 overrider) <0xab3dfc + 0x00038> in <filename unknown>:0
at Realms.SharedRealmHandle.BeginTransaction () <0xab21c8 + 0x00043> in <filename unknown>:0
at Realms.Transaction..ctor (Realms.SharedRealmHandle sharedRealmHandle) <0xab9ad8 + 0x0002f> in <filename unknown>:0
at Realms.Realm.BeginWrite () <0xab7624 + 0x0003f> in <filename unknown>:0
at Realms.Realm.Write (System.Action action) <0xab7674 + 0x0002b> in <filename unknown>:0
at MyApp.iOS.CollectionViewSource+<GetCell>c__AnonStorey0.<>m__2 (System.Threading.Tasks.Task`1 value) <0x327c34 + 0x001b3> in <filename unknown>:0
at System.Threading.Tasks.ContinuationTaskFromResultTask`1[TAntecedentResult].InnerInvoke () <0x532fa8 + 0x00077> in <filename unknown>:0
at System.Threading.Tasks.Task.Execute () <0x52ed88 + 0x0005b> in <filename unknown>:0

我确信我的应用程序一次只打开了一个写入事务。我认为唯一可能导致问题的是 Realm 事务没有正确关闭。我的代码是这样的:
// this is called at some point in the app, when entering in a specific view controller
var transaction = realm.BeginWrite ();

关闭这个 Controller 的代码是:
public void Close(bool saveChanges) {
if (saveChanges) {
transaction.Commit ();
transaction.Dispose ();
}
else {
transaction.Rollback ();
transaction.Dispose ();
}

// Ignore the fact that this code can be included in the previous section
// It is just for making clear that after closing the transaction I can make
// a simple change to the objects
if (saveChanges) {
realm.Write(() => {
model.id = 1;
}
}

NavigationController.PopToRootViewController (true);
}

当 Root View Controller 显示时,它会访问 Realm 数据,此时就是崩溃发生的时刻。

关于这里可能发生的事情的任何提示?

最佳答案

我的第一 react 是,最有可能的是您的 Close并不总是被调用。有什么方法可以让您的应用过渡到另一个屏幕,或重新输入这个,不用打Close ?

然而

查看您的堆栈跟踪,让我印象深刻的是您显然正在调用 Write来自单元格 setter/getter ,这就是导致崩溃的原因:
MyApp.iOS.CollectionViewSource+<GetCell...
您是否有在该屏幕打开时正在刷新的列表?

更新

从下面的评论中需要注意一件事,这可能会让您感到困惑。

两个 在 Realm 中启动写入事务的方法。
BeginTransaction创建并返回一个事务,所以你有机会调用 Rollback .
Realm.Write( lambda )调用还会创建一个临时写入事务 所以你不能在同一个线程上调用它。

我认为这可能是造成困惑的原因,如果我们应该更新文档并尝试改进异常消息以在 Write 中提取,我将与团队讨论。在这种情况下被调用。

关于xamarin - 获取异常 : "Already in a write transaction" in Realm for Xamarin iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39583502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com