使用NSPredicate按日期筛选核心数据不返回任何内容 [英] coredata filtering by date using NSPredicate returns nothing

查看:117
本文介绍了使用NSPredicate按日期筛选核心数据不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是这个coredata的新人。我有两个实体CategoryList和ExpenseList。 CategoryList由两个属性组成

1.dates(nsdate)

2.categories(nsstring)

和许多与ExpenseList的关系,关系名称是 costList
ExpenseList由
1.amountSpent(float)组成。

我只想了解coredata中的关系特征。当我试图创建一个nspredicate过滤数据的特定日期,它不返回任何东西。我只想过滤类别列表实体中的特定日期的类别属性内容。这是我的代码

Hi i am new to this coredata. I have two entities CategoryList and ExpenseList. CategoryList consists of two attributes
1.dates (nsdate)
2.categories (nsstring)
and in to many relation with ExpenseList, relationship name is "expenseList" ExpenseList consists of 1.amountSpent (float).
I just wanted to know learn about relationship features in coredata. When i tried to create a nspredicate to filter the data by a specific date, its returning nothing. I just wanted to filter the categories attribute content in the CategoryList entity for a specific date. Here is my code

-(void) callingByDate
{


NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

NSEntityDescription *categoryL = [NSEntityDescription entityForName:@"CategoryList" inManagedObjectContext:managedObjectContext];

NSSortDescriptor *dateSort = [[NSSortDescriptor alloc]initWithKey:@"date" ascending:YES];

NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:dateSort, nil];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dates == %@",date];



NSFetchRequest *dateFetch = [[NSFetchRequest alloc]init];



[dateFetch setSortDescriptors:sortDescriptors];

[dateFetch setEntity:categoryL];

[dateFetch setPredicate:predicate];



NSMutableArray *results = [[managedObjectContext executeFetchRequest:dateFetch error:nil] mutableCopy];

if([results count] > 0)

    NSLog(@"results found");

else

    NSLog(@"no results found");

}



<未找到结果'。我不知道为什么。提前感谢

when i execute this code in the nslog area its showing 'no results found'. I could not figure out why . Thanks in advance

这里是我的代码,用于查找结束日期和开始日期

here is my code to find the end and start of the day

-(NSDate *)beginningOfDay:(NSDate *)date;

 {

 NSDate *dat = [NSDate date];

 NSCalendar *cal = [NSCalendar currentCalendar];

 NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:dat];

[components setHour:0];

[components setMinute:0];

[components setSecond:0];



return [cal dateFromComponents:components];





 }



-(NSDate *)endOfDay:(NSDate *)date;

{

NSDate *dat = [NSDate date];

NSCalendar *cal = [NSCalendar currentCalendar];

NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:dat];

[components setHour:23];

[components setMinute:59];

[components setSecond:59];



return [cal dateFromComponents:components];

当i nslog时,结果显示为
startday -2013-04-30 18: 30:00 +0000
endOfDay -2013-05-01 18:29:59 +0000

when i nslog, the result showing is startday -2013-04-30 18:30:00 +0000 endOfDay -2013-05-01 18:29:59 +0000

推荐答案

date 实际上是一个时间戳,所以 == 与它只会返回具有确切的时间戳的项目。

date is actually a timestamp, so doing == with it will only return items which have that exact timestamp.

要获取特定日期的所有条目,您需要获取 NSDate 作为一天的开始( startOfDay )和 NSDate endOfDay )然后修改谓词以使用以下两个值之间的日期:

To get all the entries on a specific date you'll want to get the NSDate for the start of the day (startOfDay), and the NSDate for the end of the day (endOfDay) then modify the predicate to get dates between those two values using:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dates >= %@ AND dates >= %@", startOfDay, endOfDay];

要计算 startOfDay endOfDay ,您可以手动执行,也可以使用其他一些库来计算它,例如 https://github.com/erica/NSDate-Extensions

To calculate the startOfDay and endOfDay you can either do it yourself manually or there are some other libraries you can use to calculate it such as https://github.com/erica/NSDate-Extensions.

这篇关于使用NSPredicate按日期筛选核心数据不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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