Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. - (BOOL)countryExistsWithName:(NSString *)countryName{
  2.  
  3. NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Countries"];
  4.  
  5. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"countryName == %@, countryName];
  6. [fetchRequest setPredicate:predicate];
  7.  
  8. NSError *error;
  9. NSUInteger count = [self.managedObjectContext countForFetchRequest:fetchRequest error:&error];
  10.  
  11. if (count == NSNotFound) {
  12. // error
  13. NSLog(@"error");
  14. return NO;
  15. }
  16. if (count > 0) {
  17. // at least one country found
  18. return YES;
  19. }
  20.  
  21. return NO;
  22. }
  23.  
  24. - (NSInteger )countAllCountries{
  25.  
  26. // We use an NSPredicate combined with the fetchedResultsController to perform the search
  27. NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Countries"];
  28.  
  29. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"countryName" ascending:YES];
  30. fetchRequest.sortDescriptors = @[sortDescriptor];
  31.  
  32. NSError *error;
  33. NSUInteger count = [self.managedObjectContext countForFetchRequest:fetchRequest error:&error];
  34.  
  35. if (count == NSNotFound) {
  36. // error
  37. NSLog(@"error");
  38. return 0;
  39. }
  40. if (count > 0) {
  41. // at least one country found
  42. return count;
  43. }
  44.  
  45. return count;
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement