1
私のコアデータテーブルには、このようなものです。その結果は次のようにする必要があります:は明確な結果がコアデータから一つのフィールドに基づいて取得する方法を
これは私のコードです:
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"MyCoreDataObject" inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"MESSAGE <> ''"];
request.predicate = predicate;
NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"TIME" ascending:NO];
NSArray *sortDescriptors = @[sd1];
[request setSortDescriptors:sortDescriptors];
[request setEntity:entityDescription];
NSError *error;
NSArray *msgs = [moc executeFetchRequest:request error:&error];
私はすべてのソートされたデータ(最初に最新のメッセージ)を取得します。 しかし、送信者に基づいて一意の値を取得するにはどうすればよいですか?各送信者の最新のメッセージのみ?
基本的に私は、このSQLの同等のものが必要です。
SELECT message, sender, MAX(time) as maxTime FROM myTable GROUP BY sender;