一些奇怪的结果是获取请求 - 核心数据

时间:2014-02-06 12:32:59

标签: ios objective-c core-data nsfetchrequest

我有以下谓词,但是在获取请求后我得到了一些非常奇怪的结果。

谓词:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parkingEndTime > %f",floor(timestamp)];

示例:

  • timestamp = 1 391 689 631
  • 结果:1​​ 391 689 612
  • 1 391 689 625

这是完整的代码:

+ (NSArray *)getParkedCars {
    AppDelegate *myAppDelegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *ctx = [myAppDelegate getThreadSafeManagedObjectContext];

    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Car"];

    NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970];
    NSLog(@"Parking End Time: %f",floor(timestamp));
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parkingEndTime > %f",floor(timestamp)];
    [request setPredicate:predicate];

    NSError *error = nil;
    NSArray *result = [ctx executeFetchRequest:request error:&error];
    NSLog(@"Fetched request: %@",[result firstObject]);
    return result;
}

1 个答案:

答案 0 :(得分:0)

通过在比较之前将其转换为NSNumber来修复:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parkingEndTime > %@",@(floor(timestamp))];