核心数据问题 - 检查项目是否存在 [英] Core Data issue - checking if item exists

查看:98
本文介绍了核心数据问题 - 检查项目是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理从NSScreenCast处理导入到Core Data应用程序的代码示例。 (https://github.com/subdigital/nsscreencast/tree/master/012-core-data-importing-data/BeerBrowser)。我有大部分工作的例子。我可以推动刷新按钮,它解析json并将其导入到数据库。但是,每次按下刷新按钮,它都会重新添加相同的数据。我已经跟踪到下面的代码。

I am working on a code example from NSScreenCast that deals with importing to a Core Data application. (https://github.com/subdigital/nsscreencast/tree/master/012-core-data-importing-data/BeerBrowser). I have the example working for the most part. I am able to push the refresh button, it parses the json and imports it in to the database. However, every time I press the refresh button it re-adds the same data. I have traced it down to the following code.

+ (Brewery *)breweryWithServerId:(NSInteger)serverId usingManagedObjectContext:(NSManagedObjectContext *)moc {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[Brewery entityName]];

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"serverId = %d", serverId]];
[fetchRequest setFetchLimit:1];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:fetchRequest error:&error];
NSLog(@"results: %@", results);
if (error) {
    NSLog(@"ERROR: %@ %@", [error localizedDescription], [error userInfo]);
    exit(1);
}

if ([results count] == 0) {
    return nil;
}
NSLog(@"results objectAtIndex:0 = %@", [results objectAtIndex:0]);

return [results objectAtIndex:0];
}

这种方法会尝试查看项目是否已经存在在数据库中。如果此调用返回nil,那么MasterViewController中的代码将再次将其添加到数据库。我做了一些调试,并且serverId获得通过。此外,fetchrequest似乎是有效的(无法调试它是肯定的)。正如你可以看到我已经为结果放了一个NSLog,但它返回一个空结果。所以,它然后去,如果结果计数是0,它是,它返回nil。因此我的问题。我没有看到任何其他地方这个问题可能是一个问题。有任何想法吗?

What happens is this method is that it tries to see if the item already exists in the database. If this call returns nil, then the code in MasterViewController adds it again to the database. I have done some debugging and the serverId does get passed. Also, the fetchrequest seems to be valid (haven't been able to debug it to be sure). As you can see I have put a NSLog for the results, but it returns an empty result. So, it then goes to if the results count is 0 which it is, it returns nil. Thus my issue. I don't see any where else where this issue could be a problem. Any thoughts?

Mike Riley

Mike Riley

推荐答案

,你可以尝试:

+ (Brewery *)breweryWithServerId:(NSInteger)serverId usingManagedObjectContext:(NSManagedObjectContext *)moc {
  NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[Brewery entityName]];
  [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"serverId == %d", serverId]];
  [fetchRequest setFetchLimit:1];

  NSError *error = nil;

  // if there's no object fetched, return nil
  if ([managedObjectContext countForFetchRequest:fetchRequest error:&error] == 0) {
    NSLog(@"!!!WARN: NO Object Matches.");
    return nil;
  }

  // fetch your object
  Brewery *result = [[moc executeFetchRequest:fetchRequest error:&error] lastObject];
  if (error != nil) {
    NSLog(@"ERROR: %@ %@", [error localizedDescription], [error userInfo]);
    return nil;
  }

  NSLog(@"results objectAtIndex:0 = %@", result);
  return result;
}

注意: -countForFetchRequest:error:更有效,因为它只返回给定的获取请求将返回的对象数。您可以使用此方法检查是否有匹配的对象。

Note: -countForFetchRequest:error: is more efficient as it only 'returns the number of objects a given fetch request would have returned'. You can use this method to check whether there's a object that matches.

这篇关于核心数据问题 - 检查项目是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆