書き込みトランザクション外でオブジェクトを変更しようとしています
-
21-12-2019 - |
質問
したがって、なぜこのエラーが発生するのかわかりません。エラーメッセージは次のとおりです。
* キャッチされなかった例外「RLMException」によりアプリを終了します。理由:「書き込みトランザクション以外でオブジェクトを変更しようとする - 最初にrlmrealmインスタンスでbeginwriteTransactionを呼び出します。」 * 最初にスローされる呼び出しスタック:(0x2f7b0f83 0x39f61ccf 0xc46ef 0xc3c23 0xc0c9d 0xb3e73 0x3a4449833 0x3a449ded 0x3a44a297 0x3a45c888d 0x3a45cb21 0x3a8bd3a8bd38bbd3a8bbd38bedidイリブ: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];
}
}
書き込みトランザクション内で書き込みを行っていることは明らかです。これはレルムのバグですか?それとも何かが足りないのでしょうか...?
解決
の beginWriteTransaction
そして commitWriteTransaction
変更しているオブジェクトが存在するのと同じレルムで呼び出す必要があります。電話をかけるたびに [RLMRealm defaultRealm]
, 、新しい領域を手に入れています。これは、今ある領域と同じではありません。 self.chatSession
. 。これを修正するには、まず次のことを確認します。 self.chatSession
のレルムはあなたのレルムと同じキュー上にあります queueForWrites
(私は仮定しています self.chatSession
です RLMObject
, 、 もちろん)。次に、代わりにブロック内で次の操作を実行します。
[self.chatSession.realm beginWriteTransaction];
self.chatSession.myAlias = alertTextField.text;
[self.chatSession.realm commitWriteTransaction];