2014-09-02 4 views
0

В моих основных данных fetchRequest некоторые из возвращаемых записей не содержат ничего, кроме нулевого, но я не вижу никаких ошибок при отправке данных. Я пытаюсь выяснить, что даже вызывает эту проблему в первую очередь.Основные данные содержат нулевые объекты

Вот мой код для первой выборки данных и сохранения его

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

Наконец-то, это снайпер из дубликата. Не каждая запись возвращается нуль

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

+0

также, для опрятности вы можете заменить "[dict objectForKey: @" type "];" с dict [@ "type"]; – Rambatino

+0

getAllCorpCards просто передает CorpCards для строки фракции. Целью метода является возможность получить любой объект и просто изменить имя строки. – TheM00s3

ответ

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

Смежные вопросы