0

I am trying to parse google weather API. I am using nsxml parser to get dates, weather etc from the api and then I store them in Core Data.

What I am trying to do is extract dates up from the parser, match them with the current date and then store only as many information we need to store.

Say, today's date is 08/09/2011 and a date from the parse matches. I wish to store only 2 information from the parser into Core Data. I am trying to store only those dates but I am getting all 4 information to be stored into Core Data.

If I give 08/11/2011, I should be only getting 3 days of information not 4. But I am unable to do that. I am posting my sample code. I am using testcase to check my application.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict

{  
if ([@"forecast_date" isEqualToString:elementName]) 
{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
     startDate = [formatter dateFromString:[attributeDict objectForKey:@"data"]];

    [formatter release];

 } 
  else if ([@"forecast_conditions" isEqualToString:elementName])
  {

    isParsingForecast = YES;
    isParsingInformation = NO;
    isParsingCurrent = NO;

     newdate=[startDate addTimeInterval:60*60*24*[forecastConditions count]];
     NSMutableDictionary *fields = [[NSMutableDictionary alloc] init];
    [fields setObject:newdate forKey:@"date"];
    [fields setObject:city    forKey:@"city"];
    [fields setObject:state   forKey:@"state"];
    [fields setObject:country forKey:@"country"];
      [fields setObject:startDate forKey:@"startdate"];
      //[fields setObject: forKey:<#(id)#>]
    [forecastConditions addObject:fields];
    [fields release];

}


 else if (isParsingForecast) {

    NSMutableDictionary *fields = [forecastConditions lastObject];
    NSLog(@"dic is : %@ \n\n",fields);

    [fields setObject:[attributeDict objectForKey:@"data"] forKey:elementName];

}

}

Posted my whole code here http://www.iphonedevsdk.com/forum/iphone-sdk-development/87475-coredata-storing-more-values-than-what-required-store-database.html#post363100

TechZen
  • 64,370
  • 15
  • 118
  • 145
new
  • 1
  • 1
  • What do you mean when you say, I wish to store only 2 information? What exactly is information? You could give us readouts of what values are in core data and then edit them to what you would like it to look like? – Karoly S Aug 09 '11 at 14:56
  • My information is what data I am getting from parser..I need to check them by dates and insert them into coredata. – new Aug 09 '11 at 15:09
  • I posted my whole code on the link...I couldn't post here I am having some issues. – new Aug 09 '11 at 15:09

1 Answers1

0

The code at the link is unformatted and very hard to read. However, I did find one major problem.

This predicate will always fail:

  predicate = [NSPredicate predicateWithFormat:
  @"city == %@ and state == %@ and country == %@ and date==%@ and date==%@", city, state, country,startDate,endDate];

... if startDate and endDate values are not identical. You can't test the same key name against two different values and ever expect it to pass.

Since, a fetch returns only those objects who pass the predicate, a predicate that always fails always returns zero objects.

Since you apparently are using the predicate to find existing objects that already contain the parsed data, the always failing predicate means your code always thinks it needs to create a new managed object. That is why your object graph fills up with objects with duplicate values.

TechZen
  • 64,370
  • 15
  • 118
  • 145