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

*由于未捕获异常"RLMException"而终止应用程序,原因:'尝试在写事务之外修改对象-首先在RLMRealm实例上调用beginWriteTransaction。' * 第一次抛出调用堆栈:(0x2f7b0f83 0x39f61ccf0xc46ef0xc3c23 0xc0c9d0xb3e73 0x3a449833 0x3a449ded0x3a44a297 0x3a45c88d0x3a45cb21 0x3a58bbd3 0x3a58ba98) libc++abi。迪利布:以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的bug吗?还是我错过了什么。..?

有帮助吗?

解决方案

beginWriteTransactioncommitWriteTransaction 必须在你正在修改的对象所在的同一个领域被调用。每次你打电话 [RLMRealm defaultRealm], ,你得到了一个新的境界。这不会是同一个领域 self.chatSession.要解决这个问题,首先确认 self.chatSession你的领域和你的领域在同一个队列上。 queueForWrites (我假设 self.chatSession 是一个 RLMObject, ,当然)。然后,只需在块内执行以下操作即可:

[self.chatSession.realm beginWriteTransaction];
self.chatSession.myAlias = alertTextField.text;
[self.chatSession.realm commitWriteTransaction];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top