在我的核心數據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
你如何在應用程序委託上調用「getAllCorpCards」? – Rambatino 2014-09-02 01:19:43
也可以替換爲「[dict objectForKey:@」type「];」與字典[@「type」]; – Rambatino 2014-09-02 01:20:15
getAllCorpCards只傳遞派生字符串的CorpCards。該方法的目的是能夠獲得任何實體,只需更改字符串名稱即可。 – TheM00s3 2014-09-02 01:21:30