0

In CoreData Model I have"customers" entity with follow columns: custostomer_id , name , city , phone

It has the follow data in it:

1                Mary        Los-Angeles     054-112233
2                John        New-York        054-334455
3                Anna        Los-Angeles     054-445566

How can I get the List of all cities of customers where each city appears only once. For above example it should be {Los-Angeles, New-Yoirk}.

This is my code:

  AppDelegate * delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
  NSManagedObjectContext *context = delegate.managedObjectContext ;
  NSEntityDescription *entityDescN = [NSEntityDescription entityForName:@"customers" inManagedObjectContext:context];
  NSFetchRequest *requestN = [[NSFetchRequest alloc] init];
  [requestN setEntity:entityDescN ];
  NSPredicate *predN = [NSPredicate predicateWithFormat:@"?????????????"];
  [requestN setPredicate:predN];
  NSArray * objectsN = [context executeFetchRequest: requestN error:&error];

I mean what should be instead of "???" in NSPredicate definition? I tried to write this code also with NSExpression, but still didn't succeeded.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Tatiana
  • 390
  • 8
  • 23
  • Read "Fetching Distinct Values" section https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/CoreDataSnippets/Articles/fetchExpressions.html http://stackoverflow.com/questions/16424720/nspredicate-fetch-one-of-each-kind – Rahul Wakade Sep 28 '13 at 07:20

1 Answers1

0

//write this string in predicate

NSPredicate *predN = [NSPredicate predicateWithFormat:@"SELECT city FROM customers GROUP BY  city"];
Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36