I am starting with iphone development and am facing an issue with core data.
I have a model gathering several entities such as employee, project, project type, etc. On startup, I create several entities that I persist through the core data framework. No problem.
The i开发者_高级运维ssue raises when I want to display a list of projects basing on a tab the user selects inside the UIToolbar
. I've set the argument to show every sql request launched (-com.apple.CoreData.SQLDebug 1
) and what is really awkward is that the query shown in the console give me results when I access the db on my side through sqlite but from core data, nope, 0 rows returned...
Anyone faced similar issue?
Here is the code used to retrieve the list of projects:
NSArray* retVal = nil;
NSError *error = nil;
NSManagedObjectContext *moc = "context retrieved";
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:moc];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
retVal = [moc executeFetchRequest:request error:&error];
NSLog(@"Project type: %@", [theProjType valueForKey:@"projectTypeName"]);
NSLog(@"Employee number: %@", [theEmployee valueForKey:@"employeeNumber"]);
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"ANY myEmployees.employeeNumber = %d AND myProjectType.projectTypeName = %d", [theEmployee valueForKey:@"employeeNumber"], [theProjType valueForKey:@"projectTypeName"]];
[request setPredicate:predicate];
retVal = [moc executeFetchRequest:request error:&error];
This code generates this sql request (which returns results when launched on the sqlite db):
SELECT DISTINCT 0, t0.Z_PK, t0.Z_OPT, t0.ZPROJECTNUMBER, t0.ZISEDITABLE, t0.ZPROJECTNAME, t0.ZISDELETABLE, t0.ZPROJECTEND, t0.ZPROJECTID, t0.ZPROJECTSTART, t0.ZCUSTOMERORDERNR, t0.ZMYCUSTOMER, t0.ZMYCOSTCENTRE, t0.ZMYPROJECTTYPE, t0.ZMYTRAVELTIMES FROM ZPROJECT t0 JOIN Z_4MYPROJECTS t1 ON t0.Z_PK = t1.Z_10MYPROJECTS1 JOIN ZEMPLOYEE t2 ON t1.Z_4MYEMPLOYEES = t2.Z_PK JOIN ZPROJECTTYPE t3 ON t0.ZMYPROJECTTYPE = t3.Z_PK WHERE ( t2.ZEMPLOYEENUMBER = ? AND t3.ZPROJECTTYPENAME = ?)
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"ANY myEmployees.employeeNumber = %d AND myProjectType.projectTypeName = %d", [theEmployee valueForKey:@"employeeNumber"], [theProjType valueForKey:@"projectTypeName"]];
Are you sure this is correct? valueForKey: always returns a NSObject but your placeholder is %d. This should create strange behaviour. Try %@ instead
精彩评论