带有 groupBy 的 nspredicate 不喜欢带有 nsdate 的谓词比较
Posted
技术标签:
【中文标题】带有 groupBy 的 nspredicate 不喜欢带有 nsdate 的谓词比较【英文标题】:nspredicate with groupBy don't like predicate with nsdate compare 【发布时间】:2013-02-11 22:17:22 【问题描述】:这段代码:
entity = [NSEntityDescription entityForName:@"PerDestinationsStat" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
//NSFetchedPropertyDescription *rateCodeDesc = [entity.propertiesByName objectForKey:@"rateCode"];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:[entity.propertiesByName objectForKey:@"rateCode"],[entity.propertiesByName objectForKey:@"rateCountry"],[entity.propertiesByName objectForKey:@"rateSpecific"],[entity.propertiesByName objectForKey:@"ratePrice"],[entity.propertiesByName objectForKey:@"allCalls"],[entity.propertiesByName objectForKey:@"acd"],[entity.propertiesByName objectForKey:@"asr"],[entity.propertiesByName objectForKey:@"cost"],[entity.propertiesByName objectForKey:@"duration"],[entity.propertiesByName objectForKey:@"income"],[entity.propertiesByName objectForKey:@"succesefulCalls"],nil]];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"rateCode",@"rateCountry",@"rateSpecific",@"ratePrice",@"allCalls",@"acd",@"asr",@"cost",@"duration",@"income",@"succesefulCalls",nil]];
predicate = [NSPredicate predicateWithFormat:@"(perHourStat.dateFrom > %@) and (perHourStat.dateTo < %@) and (perHourStat.originator.company == %@)",from,to,selected];
[fetchRequest setPredicate:predicate];
fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
找不到对象,但如果我使用:
predicate = [NSPredicate predicateWithFormat:@" (perHourStat.originator.company == %@)",selected];
找到对象:
> Printing description of fetchedObjects: <_PFArray 0x100664fb0>(
> <PerHourStat: 0x101233810> (entity: PerHourStat; id: 0x10122f310
> <x-coredata://BBDDF3D7-EA50-4290-B03A-05349E74467E/PerHourStat/p1> ;
> data:
> acd = "0.03688591122627258";
> allCalls = 3;
> asr = 1;
> cost = "0.1327892804145813";
> dateFrom = "2013-02-11 21:00:00 +0000";
> dateTo = "2013-02-11 21:59:59 +0000";
> duration = "6.639464020729065";
> income = "0.1991839206218719";
> originator = "0x101236000 <x-coredata://BBDDF3D7-EA50-4290-B03A-05349E74467E/Originator/p1>";
> perDestinationsStat = "<relationship fault: 0x104d2b9b0 'perDestinationsStat'>";
> succesefulCalls = 3;
> terminator = nil; )
from/to 与找到的对象中的 dateFrom 匹配:
> description of from: 2013-02-11 22:00:00 +0000
> description of to: 2013-02-12 21:59:59 +0000
【问题讨论】:
看起来您的日期比较应该失败,这可以解释为什么没有找到对象。 【参考方案1】:您正在比较同一日期21:00
> 22:00
。这将清楚地匹配 0 条记录。
【讨论】:
以上是关于带有 groupBy 的 nspredicate 不喜欢带有 nsdate 的谓词比较的主要内容,如果未能解决你的问题,请参考以下文章
带有 NSPredicate 的 NSFetchedResultsController 未更新
带有 NSPredicate 的 NSFetchRequest 不返回任何结果