有時(很少發生但是發生)當試圖用屬性或AFnetworking Block內部修改我的模型對象時,出現錯誤Object has been deleted or invalidated.
。任何人都可以幫助我找到我做錯了什麼嗎?錯誤:對象已被刪除或無效。 (領域)
錯誤 - 案例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];
}
}
感謝。
而不是發送模型我改變發送模型的主鍵,每次有必要我試着找到它之前。崩潰消失了,它的工作正常,謝謝! –