试图在写入事务之外修改对象 [英] Attempting to Modify Object Outside of Write Transaction

查看:80
本文介绍了试图在写入事务之外修改对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我不知道为什么我会收到这个错误。错误消息如下:

So I have no idea why I am getting this error. The error message is as follows:

*由于未捕获的异常'RLMException'而终止应用程序,原因:'尝试修改写事务之外的对象 - 调用首先在RLMRealm实例上使用beginWriteTransaction。'
*
第一次抛出调用堆栈:
(0x2f7b0f83 0x39f61ccf 0xc46ef 0xc3c23 0xc0c9d 0xb3e73 0x3a449833 0x3a449ded 0x3a44a297 0x3a45c88d 0x3a45cb21 0x3a58bbd3 0x3a58ba98)
libc ++ abi。 dylib:以NSException类型的未捕获异常终止

* Terminating app due to uncaught exception 'RLMException', reason: 'Attempting to modify object outside of a write transaction - call beginWriteTransaction on a RLMRealm instance first.' * First throw call stack: (0x2f7b0f83 0x39f61ccf 0xc46ef 0xc3c23 0xc0c9d 0xb3e73 0x3a449833 0x3a449ded 0x3a44a297 0x3a45c88d 0x3a45cb21 0x3a58bbd3 0x3a58ba98) libc++abi.dylib: terminating with uncaught exception of type NSException

执行此代码时抛出。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UITextField * alertTextField = [alertView textFieldAtIndex:0];
    if (![self.chatSession.theirAlias isEqualToString:alertTextField.text]) {
        self.sender = alertTextField.text;
        dispatch_queue_t queue = ((AppDelegate *)[UIApplication sharedApplication].delegate).queueForWrites;
        dispatch_async(queue, ^{
            [[RLMRealm defaultRealm] beginWriteTransaction];
            self.chatSession.myAlias = alertTextField.text; // This is the line where the error is thrown
            [[RLMRealm defaultRealm] commitWriteTransaction];
        });
    } else {
        [self promptForAliasAfterRejection];
    }
}

我很清楚我写的是写的交易。这是Realm的错误吗?或者我错过了什么......?

It's pretty clear that I am writing inside of a write transaction. Is this a bug with Realm? Or am I missing something...?

推荐答案

beginWriteTransaction commitWriteTransaction 必须在您正在修改的对象所在的同一领域中调用。每次调用 [RLMRealm defaultRealm] ,你正在获得一个新的领域。这与 self.chatSession 中的区域不同。要解决此问题,请首先确认 self.chatSession 的域与 queueForWrites 位于同一队列中(我是m假设 self.chatSession 当然是 RLMObject 。然后,只需在块内执行以下操作:

The beginWriteTransaction and commitWriteTransaction have to be called on the same realm the object you're modifying is in. Each time you call [RLMRealm defaultRealm], you're getting a new realm. This is not going to be the same realm that's in self.chatSession. To fix this, first confirm that self.chatSession's realm is on the same queue as your queueForWrites (I'm assuming self.chatSession is a RLMObject, of course). Then, just do the following inside the block instead:

[self.chatSession.realm beginWriteTransaction];
self.chatSession.myAlias = alertTextField.text;
[self.chatSession.realm commitWriteTransaction];

这篇关于试图在写入事务之外修改对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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