ときどき(まれに発生する)モデルオブジェクトをプロパティまたはAFnetworkingブロック内で修正しようとすると、エラーObject has been deleted or invalidated.
が発生することがあります。誰かが私が間違っていることを見つけるのを助けることができますか?エラー:オブジェクトが削除または無効化されました。 (Realm)
エラー - ケース1:
コード:
- (void)myFunction {
Model *model = [Model objectForPrimaryKey:1];
if (model) {
[self updateModel:model];
}
}
- (void)updateModel:(Model *)model {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager PUT:@"http://www.example.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
[[RLMRealm defaultRealm] beginWriteTransaction];
model.updated = YES; // Crash: Object has been deleted or invalidated.
[[RLMRealm defaultRealm] commitWriteTransaction];
} failure:nil];
}
エラー - ケース2:
プロパティ:
@property (strong, nonatomic) Model *model;
コード:
- (void)myFunction {
Model *model = [Model objectForPrimaryKey:1];
if (model) {
self.model = model;
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Would you like to edit the model?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alert show];
}
}
UIAlertViewのデリゲート:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[[RLMRealm defaultRealm] beginWriteTransaction];
self.model.updated = YES; // Crash: Object has been deleted or invalidated.
[[RLMRealm defaultRealm] commitWriteTransaction];
}
}
感謝。
モデルを送信するのではなく、Modelプライマリキーを送信するように変更したので、必要になる度に前に見つけようとします。クラッシュがなくなり、正常に動作しています、ありがとう! –