2014-09-02 153 views
0

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

你如何在应用程序委托上调用“getAllCorpCards”? – Rambatino 2014-09-02 01:19:43

+0

也可以替换为“[dict objectForKey:@”type“];”与字典[@“type”]; – Rambatino 2014-09-02 01:20:15

+0

getAllCorpCards只传递派生字符串的CorpCards。该方法的目的是能够获得任何实体,只需更改字符串名称即可。 – TheM00s3 2014-09-02 01:21:30

回答

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

,所以你只在您需要时创建新的对象。

+0

非常感谢,它确实解决了这个问题。 – TheM00s3 2014-09-02 01:30:19