gpt4 book ai didi

ios - 核心数据包含空实体

转载 作者:行者123 更新时间:2023-11-28 21:58:56 25 4
gpt4 key购买 nike

在我的核心数据 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);
}
}

最后,这里是执行在委托(delegate)中调用的获取的方法。

-(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;
}

最后,这是来自配音的片段。并非每个条目都返回 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

最佳答案

你的问题在这里 -

  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];

这样您只在需要时才创建新对象。

关于ios - 核心数据包含空实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25614562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com