核心数据:谓词在实例化后为零

时间:2012-08-17 15:07:53

标签: iphone ios5 core-data iphonecoredatarecipes

我有一个带有简单Core Data架构的iOS程序: Credata schema

并且我喜欢得到第一个和最后一个点的方法,所以我将这个代码用于最后一个:

NSFetchRequest * lastPointFetch = [[NSFetchRequest alloc] init];
[lastPointFetch setEntity:[NSEntityDescription entityForName:@"PointsForTrack"
                                 inManagedObjectContext:appDelegate.managedObjectContext]];
NSPredicate * pred = [NSPredicate predicateWithFormat:@"ALL Track == %@", ((Track*)self.detailItem)];
[lastPointFetch setPredicate:pred];
[lastPointFetch setFetchLimit:(NSUInteger)1];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[lastPointFetch setSortDescriptors:sortDescriptors];


NSArray *fetchResults = [appDelegate.managedObjectContext executeFetchRequest:lastPointFetch
                                                                            error:&error];
if ([fetchResults count]>0){
    PointsForTrack *result = [fetchResults objectAtIndex:0];
    NSLog(@"Point in track: %@", [dateFormat stringFromDate:result.timeStamp]);
}

但这是我形成框架的错误:

2012-08-17 17:00:59.992 TrackMe[16799:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate (null)'

***第一次抛出调用堆栈: (0x376ef6c3 0x3996897f 0x3582558f 0x358252b5 0x35824ecd 0x35824647 0x35824111 0x35823529 0x35821e23 0x3d56d 0x3cf63 0x3d0d5 0x391258d5 0x39131d75 0x39131a81 0x39120163 0x3911fe03 0x39180abd 0x39203529 0x371e4aaf 0x376c49ff 0x376c46b1 0x376c3321 0x3763639d 0x37636229 0x35feb31b 0x391128f9 0x3a661 0x39ad0b20) libc ++ abi.dylib:terminate调用抛出异常

知道为什么会这样吗?

先谢谢

更新: 我打印了NSPRedicate的说明并记录了所有内容:

2012-08-17 20:59:39.081 TrackMe[8126:c07] NSPredicate ALL track == <Track: 0x778f6b0> (entity: Track; id: 0x778e0d0 <x-coredata://58DE6A89-8060-4D63-90DD-C31A09F53C79/Track/p1> ; data: {
creationDate = "2012-08-17 18:56:23 +0000";
inAcquisition = nil;
name = "New Track";
points = "<relationship fault: 0x77953f0 'points'>";

}) 2012-08-17 21:00:04.679 TrackMe [8126:c07] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'不支持的谓词(null)' * 第一次抛出调用堆栈: (0x1b41552 0x1477e7e 0x118c8f9 0x118c312 0x118c0e0 0x118beb5 0x118b697 0x118b184 0x118a2fd 0x1188259 0x6a19 0x61e0 0x6414 0x1a2c2c 0x1a2f5b 0x1b0a20 0x1b8a0d 0x1b92fb 0x1b98f3 0x1b94e8 0x513643 0x505379 0x5053f4 0x16f019 0x16f2bd 0xb6fef3 0x1b00956 0x1b003e6 0x1ae8042 0x1ae7504 0x1ae73db 0x1a9b823 0x1a9b6a8 0xc018c 0x2a4d 0x2975) libc ++ abi.dylib:terminate调用抛出异常

1 个答案:

答案 0 :(得分:0)

如果你搞乱了谓词字符串,

NSPredicate在实例化后可能为零。

查看您的谓词,我可以看到您使用Track使用大写字母T.您的关系有一个较小的情况&#39; t&#39;但是。

那么如果你使用谓词字符串@"ALL track == %@",你会得到什么?

其次,你真的需要做这个查询吗?是否可以只从您已有的points引用导航(Track*)self.detailItem关系? (不可否认,你通过NSPredicate的方式进行排序。只需考虑一下。)