核心数据存储不必要的数据

发布于 2024-11-28 19:33:52 字数 1880 浏览 3 评论 0原文

我正在尝试解析谷歌天气 API。我使用 nsxml 解析器从 api 获取日期、天气等,然后将它们存储在 Core Data 中。

我想做的是从解析器中提取日期,将它们与当前日期匹配,然后仅存储我们需要存储的信息。

假设今天的日期是 08/09/2011,并且解析中的日期匹配。我希望仅将解析器中的 2 个信息存储到核心数据中。我试图只存储这些日期,但我将所有 4 个信息存储到核心数据中。

如果我给出 08/11/2011,我应该只得到 3 天的信息而不是 4 天。但我无法做到这一点。我正在发布我的示例代码。我正在使用测试用例来检查我的应用程序。

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

}

在这里

发布我的整个代码 http://www.iphonedevsdk.com/forum/iphone-sdk-development/87475-coredata-storing-more-values-than-what-required-store-database.html#post363100

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗笺 2024-12-05 19:33:52

链接中的代码未格式化并且很难阅读。不过,我确实发现了一个大问题。

此谓词将始终失败:

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

...如果 startDate 和 endDate 值不相同。您无法针对两个不同的值测试相同的键名称并期望它能够通过。

由于提取仅返回那些通过谓词的对象,因此始终失败的谓词始终返回零个对象。

由于您显然正在使用谓词来查找已包含已解析数据的现有对象,因此始终失败的谓词意味着您的代码始终认为它需要创建一个新的托管对象。这就是为什么您的对象图充满了具有重复值的对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文