Czasami (rzadko, ale występuje) Mam błąd Object has been deleted or invalidated.
, gdy próbuję zmodyfikować mój obiekt modelu z właściwością lub wewnątrz bloku AFnetworking. Czy ktoś może mi pomóc znaleźć to, co robię źle?Błąd: Obiekt został usunięty lub unieważniony. (Realm)
Błąd - Przypadek 1:
Kod:
- (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];
}
Błąd - Przypadek 2:
nieruchomości:
@property (strong, nonatomic) Model *model;
Kod:
- (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 Delegat:
- (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];
}
}
Dzięki.
Zamiast wysłać model I zmieniono, aby wysłać klucz podstawowy modelu i za każdym razem, gdy jest to konieczne, próbuję go znaleźć wcześniej. Awaria zniknęła i działa poprawnie, dziękuję! –