Core Data: predicate is nil after instantiation

580 views Asked by At

i have an iOS program with a simple Core Data schema: Credata schema

and into a method i like to get the first and last point so i use this code for the last:

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]);
}

but this is the error that i god form framework:

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 exception

any idea why this happen?

thank in advanced

UPDATE: i have printed the description of NSPRedicate and log all:

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] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate (null)' * First throw call stack: (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 called throwing an exception

1

There are 1 answers

4
occulus On

NSPredicate can be nil after instantiation if you've messed up the predicate string.

Looking at your predicate, I can see that you're using Track with a capital T. Your relationship has a lower case 't' however.

So what do you get if you use the predicate string @"ALL track == %@"?

Secondarily, do you really need to do this query? Is it possible instead just to navigate the points relationship from the (Track*)self.detailItem reference you already have? (Admittedly, you're getting sorting by doing it the NSPredicate way. Just something to consider.)