将sql查询替换为核心数据获取请求

时间:2013-12-28 17:28:28

标签: ios objective-c core-data

如何将sql查询替换为Core Data获取请求? 这是查询:

NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM diario GROUP BY strftime('%%d', date_time) ORDER BY date_time"];

1 个答案:

答案 0 :(得分:0)

您必须使用NSPredicate类和NSSortDescriptor对结果进行排序。那将是这样的:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"diario" inManagedObjectContext:moc];

NSPredicate *predicate = [NSPredicate predicateWithFormat:/*Your predicate here*/];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"date_time" ascending:YES];

fetchRequest.entity = entity;
fetchRequest.predicate = predicate;
fetchRequest.sortDescriptor = sort;