有时(很少发生但是发生)当试图用属性或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];
}
}
感谢。
而不是发送模型我改变发送模型的主键,每次有必要我试着找到它之前。崩溃消失了,它的工作正常,谢谢! –