在我的核心数据fetchRequest中,返回的一些条目只包含null,但是当我去保存数据时,我没有看到任何错误。我试图找出甚至导致这个问题的原因。
这是我首次获取数据并保存数据的代码
- (IBAction)getCaredBtnPressed:(id)sender {
NSString *urlString = [NSString stringWithFormat:@"http://netrunnerdb.com/api/cards/"];
NSURL *url = [NSURL URLWithString:urlString];
[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:url] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError){
NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//Array that contains parsed JSON data
for (NSDictionary *dict in dataArray){
CorpCard *newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"CorpCard" inManagedObjectContext:self.managedObjectContext];
if ([[dict objectForKey:@"side"] isEqualToString:@"Corp"] && ![[dict objectForKey:@"setname"] isEqualToString:@"Special"]){
newEntry.type = [dict objectForKey:@"type"];
newEntry.title = [dict objectForKey:@"title"];
newEntry.text = [dict objectForKey:@"text"];
newEntry.subtype = [dict objectForKey:@"subtype"];
newEntry.faction = [dict objectForKey:@"faction"];
newEntry.influence = [dict objectForKey:@"factioncost"];
newEntry.unique = [dict objectForKey:@"uniqueness"];
newEntry.limit = [dict objectForKey:@"limited"];
//Add the entries to our database
if([[dict objectForKey:@"type"] isEqualToString:@"Identity"]){
newEntry.influence =[dict objectForKey:@"influencelimit"];
newEntry.minDeckSize =[dict objectForKey:@"minimumdecksize"];
NSError *error;
if(![self.managedObjectContext save:&error]){
NSLog(@"Error occured, couldn't save: %@", [error localizedDescription]);
}
}
if([[dict objectForKey:@"type"] isEqualToString:@"Agenda"]) {
newEntry.cost = [dict objectForKey:@"advancementcost"];
newEntry.agendaPoint = [dict objectForKey:@"agendapoints"];
NSError *error;
if(![self.managedObjectContext save:&error]){
NSLog(@"Error occured, couldn't save: %@", [error localizedDescription]);
}
}
if ([[dict objectForKey:@"type"] isEqualToString:@"Operation"]){
newEntry.cost = [dict objectForKey:@"cost"];
NSError *error;
if(![self.managedObjectContext save:&error]){
NSLog(@"Error occured, couldn't save: %@", [error localizedDescription]);
}
}
if([[dict objectForKey:@"type"] isEqualToString:@"ICE"]){
newEntry.cost = [dict objectForKey:@"cost"];
newEntry.strength = [dict objectForKey:@"strength:"];
NSError *error;
if(![self.managedObjectContext save:&error]){
NSLog(@"Error occured, couldn't save: %@", [error localizedDescription]);
}
}
if ([[dict objectForKey:@"type"] isEqualToString:@"Asset"]){
newEntry.cost = [dict objectForKey:@"cost"];
newEntry.trashCost = [dict objectForKey:@"trash"];
NSError *error;
if(![self.managedObjectContext save:&error]){
NSLog(@"Error occured, couldn't save: %@", [error localizedDescription]);
}
}
if ([[dict objectForKey:@"type"] isEqualToString:@"Upgrade"]){
newEntry.cost = [dict objectForKey:@"cost"];
newEntry.trashCost = [dict objectForKey:@"trash"];
NSError *error;
if(![self.managedObjectContext save:&error]){
NSLog(@"Error occured, couldn't save: %@", [error localizedDescription]);
}
}
NSLog(@" added: %@, %@, %@, %@, %@, %@", [dict objectForKey:@"title"], [dict objectForKey:@"trash"], [dict objectForKey:@"cost"], [dict objectForKey:@"text"], [dict objectForKey:@"faction"], [dict objectForKey:@"type"]);
}
}
}
}];
}
这是我获取数据的代码
- (IBAction)prntCardsBtnPressed:(id)sender {
//print out entries of our database
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
self.corpCards = [delegate getAllCorpCards];
int i;
for (i =0; i <self.corpCards.count;i++){
CorpCard *card = [self.corpCards objectAtIndex:i];
NSLog(@"%@, %@, %@", card.title, card.text, card.type);
}
}
最后,这是执行委托中调用的提取的方法。
-(NSArray *)getAllFactionCards:(NSString *)faction{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:faction inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *cards = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
return cards;
}
最后,这是来自配音的snipet。并非每个条目都返回null
2014-09-01 18:06:33.780 NetrunnerApp[2205:60b] (null), (null), (null)
2014-09-01 18:06:33.782 NetrunnerApp[2205:60b] (null), (null), (null)
2014-09-01 18:06:33.783 NetrunnerApp[2205:60b] (null), (null), (null)
2014-09-01 18:06:33.783 NetrunnerApp[2205:60b] (null), (null), (null)
2014-09-01 18:06:33.784 NetrunnerApp[2205:60b] (null), (null), (null)
2014-09-01 18:06:33.784 NetrunnerApp[2205:60b] Melange Mining Corp., [Click], [Click], [Click]: Gain 7[Credits]., Asset
2014-09-01 18:06:33.785 NetrunnerApp[2205:60b] (null), (null), (null)
2014-09-01 18:06:33.785 NetrunnerApp[2205:60b] (null), (null), (null)
2014-09-01 18:06:33.786 NetrunnerApp[2205:60b] (null), (null), (null)
2014-09-01 18:06:33.786 NetrunnerApp[2205:60b] Scorched Earth, Play only if the runner is tagged.
Do 4 meat damage., Operation
2014-09-01 18:06:33.787 NetrunnerApp[2205:60b] Private Security Force, If the Runner is tagged, Private Security Force gains: "[Click]: Do 1 meat damage.", Agenda
2014-09-01 18:06:33.787 NetrunnerApp[2205:60b] Adonis Campaign, Put 12[Credits] from the bank on Adonis Campaign when rezzed. When there are no credits left on this card, trash it.
Take 3[Credits] from Adonis Campaign when your turn begins., Asset
2014-09-01 18:06:33.789 NetrunnerApp[2205:60b] (null), (null), (null)
2014-09-01 18:06:33.790 NetrunnerApp[2205:60b] (null), (null), (null)
2014-09-01 18:06:33.790 NetrunnerApp[2205:60b] Eli 1.0, The Runner may spend [Click] to break any subroutine on Eli 1.0.
[Subroutine] End the run.
[Subroutine] End the run., ICE
2014-09-01 18:06:33.791 NetrunnerApp[2205:60b] (null), (null), (null)
2014-09-01 18:06:33.791 NetrunnerApp[2205:60b] Haas-Bioroid: Engineering the Future, The first time you install a card each turn, gain 1[Credits]., Identity
2014-09-01 18:06:33.792 NetrunnerApp[2205:60b] NBN: Making News, 2[Recurring Credits]
Use these credits during trace attempts., Identity
2014-09-01 18:06:33.792 NetrunnerApp[2205:60b] (null), (null), (null)
2014-09-01 18:06:33.792 NetrunnerApp[2205:60b] (null), (null), (null)
2014-09-01 18:06:33.793 NetrunnerApp[2205:60b] Jinteki: Personal Evolution, Whenever an agenda is scored or stolen, do 1 net damage., Identity
答案 0 :(得分:2)
你的问题在这里 -
for (NSDictionary *dict in dataArray){
CorpCard *newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"CorpCard" inManagedObjectContext:self.managedObjectContext];
if ([[dict objectForKey:@"side"] isEqualToString:@"Corp"] && ![[dict objectForKey:@"setname"] isEqualToString:@"Special"]){
您正在对象上下文中创建一个新条目,但随后您检查字典中的值以查看是否应填充它。即使您没有填充它,新对象仍然位于您的托管对象上下文中,等待下一次调用save
。
您只需要重新构建循环的开始 -
for (NSDictionary *dict in dataArray) {
if ([[dict objectForKey:@"side"] isEqualToString:@"Corp"] && ![[dict objectForKey:@"setname"] isEqualToString:@"Special"]) {
CorpCard *newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"CorpCard" inManagedObjectContext:self.managedObjectContext];
这样您只需在需要时创建新对象。