为了拯救动物园里的主要动物,我这样做:
-(void)makePrimary:(Animal*)animal
Zoo *currentZoo = [Zoo findFirstByAttribute:@"zooId" withValue:self.currentZooId];
currentZoo.primaryAnimalId = animal.animalId;
[[NSManagedObjectContext defaultContext] saveToPersistentStoreAndWait];
DLog(@"primaryAnimalId: %d", currentZoo.primaryAnimalId.intValue); //logs "3"
}
完成此操作后,currentZoo 的 primaryAnimalId 记录为 3。我正在使用 Firefox SQLite Manager 来验证这一点。ZPRIMARYANIMALID 也是 3。
现在,我导航到应用程序的不同部分,我需要在其中显示主要动物。所以我这样做:
-(Animal*)getPrimaryAnimalForCurrentZoo
{
Zoo *currentZoo = [Zoo findFirstByAttribute:@"zooId" withValue:self.currentZooId];
DLog(@"primaryAnimalId: %d", currentZoo.primaryAnimalId.intValue); //logs 2
Animal *primaryAnimal = [Animal MR_findFirstByAttribute:@"animalId" withValue:currentZoo.primaryAnimalId];
return primaryAnimal;
}
令我懊恼的是,primaryAnimalId 得到“2”,因此返回了错误的动物。“2”是在我通过 更改它之前动物的先前值makePrimary:
。currentZooId 在这两种方法中都是 0。
我不明白的是,当我可以清楚地看到数据库中当前的正确值(在 SQLite 管理器中)时,如何从 Core Data 中获取错误的值。这怎么可能,我该如何解决?