0

我有一个带有简单核心数据模式的 iOS 程序: 创建数据架构

并进入我喜欢获得第一点和最后一点的方法,因此我将这段代码用于最后:

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)'

*** First throw call stack: (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 called throwing an例外

知道为什么会这样吗?

感谢先进

更新:我已经打印了 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 0x118c8f9c 0x118c312 0x118 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 called throwing an exception

4

1 回答 1

0

NSPredicate如果您弄乱了谓词字符串,则实例化后可以为 nil。

查看您的谓词,我可以看到您使用Track的是大写 T。但是,您的关系有一个小写的“t”。

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

其次,你真的需要做这个查询吗?是否可以仅从您已有的参考中导航points关系?(Track*)self.detailItem(诚​​然,您通过 NSPredicate 方式进行排序。只是需要考虑的事情。)

于 2012-08-17T16:31:48.713 回答