所以我不知道为什么会出现这个错误。错误信息如下:
* 由于未捕获的异常“RLMException”而终止应用程序,原因:“尝试在写入事务之外修改对象 - 首先在 RLMRealm 实例上调用 beginWriteTransaction。”
*首先抛出调用堆栈:
(0x2f7b0f83 0x39f61ccf 0xc46ef 0xc3c23 0xc0c9d 0xb3e73 0x3a449833 0x3a449ded 0x3a44a297 0x3a45c88d 0x3a45cb21 0x3a58bbd3 0x3a58巴98)
libc++abi.dylib:以 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 的错误吗?或者我错过了什么......?
The beginWriteTransaction
and commitWriteTransaction
必须在您正在修改的对象所在的同一个领域上调用。每次调用[RLMRealm defaultRealm]
,你将获得一个新的境界。这不会是同一个领域self.chatSession
。要解决此问题,首先确认self.chatSession
的领域与您在同一个队列中queueForWrites
(我假设self.chatSession
is a RLMObject
, 当然)。然后,只需在块内执行以下操作:
[self.chatSession.realm beginWriteTransaction];
self.chatSession.myAlias = alertTextField.text;
[self.chatSession.realm commitWriteTransaction];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)