Ho un problema con il recupero di un oggetto dopo l'aggiunta in una relazione. La prima volta che ho recuperato la categoria, ho sempre trovato, quindi quando ho aggiunto alla relazione le seguenti categorie non sono state trovate.Non trovato un oggetto in coredata dopo l'aggiunta a una relazione
La relazione è un Many-To-Many
.
Esempio:
- Fetch categoria con
categoryId = 10
- oggetto categoria Trovato
- aggiunta alla relazione oggetto padre
- Prossimo oggetto
Se le diverse categorie ha la stessa
id
,categoryId = 10
, non trovatoNSManagedObjectContext *private = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [private setParentContext:self.model.context]; __block NSError *error = nil; [private performBlockAndWait:^{ GPDeal *deal = [EKManagedObjectMapper objectFromExternalRepresentation:dic withMapping:[GPDeal objectMapping] inManagedObjectContext:private]; for (NSDictionary *dic in responseObject[@"response"]) { GPCategory *category; //The first time always found if ((category = [GPCategory MR_findFirstByAttribute:@"catId" withValue:dic[@"mainAttribute"] inContext:private])) { NSLog(@"Found"); [category addDealsObject:deal]; } else { NSLog(@"Not Found"); } } }]; NSError *PrivateError = nil; if (![private save:&PrivateError]) { NSLog(@"Unresolved error %@, %@", PrivateError, [PrivateError userInfo]); abort(); } if (!error) { //Save on main moc [self.model saveWithErrorBlock:^(BOOL success, NSError *error) { if (!success) { NSLog(@"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]); } }]; } else { NSLog(@"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]); }
EDIT:
risolto, credo che il mio problema era che ho dimenticato di salvare il contesto principale, alla fine di ogni iterazione.
NSManagedObjectContext *backgroundMOC = [self.model backgroundMOC:self.model.context];
[backgroundMOC performBlockAndWait:^{
for (NSDictionary *dic in responseObject[@"response"]) {
GPDeal *deal = [EKManagedObjectMapper objectFromExternalRepresentation:dic withMapping:[GPDeal objectMapping] inManagedObjectContext:backgroundMOC];
GPCategory *category;
if ((category = [GPCategory MR_findFirstByAttribute:@"catId" withValue:dic[@"mainAttribute"] inContext:backgroundMOC])) {
NSLog(@"Found with mainAttribute %@", dic[@"mainAttribute"]);
[deal addDealCategoryObject:category];
}
if([backgroundMOC hasChanges]) {
NSError * error;
[backgroundMOC save:&error];
[self.model.context performBlockAndWait:^{
if([self.model.context hasChanges]) {
NSError * error;
[self.model.context save:&error];
}
}];
}
}
}];
A quale contesto appartiene l'oggetto 'deal'? Sembra che debba essere un contesto diverso da "privato". Non si dovrebbero impostare relazioni tra oggetti in diversi contesti. Passa il ManagedObjectID e poi recuperalo nel contesto 'private' usando' objectWithID'. – pbasdf
Ho già controllato, ma 'deal' viene creato all'interno del contesto privato – brunobasas
quindi il tuo problema è solo con caregoryId = 10? La prima volta che ne hai 10 in dic [@ "mainAttribute"] trovi la categoria e la seconda volta no? Il fatto che il problema non provenga da MR_findFirstByAttribute mi dice che il problema potrebbe essere la stringa contenente il numero intero in dic [@ "mainAttribute"]. potresti NSLog (@ "ID: -% @ -") per verificare che l'ID non abbia spazi prima o dopo? – Mikael