我从iphone开发开始,面临核心数据问题。
我有一个模型收集了几个实体,如员工,项目,项目类型等。在启动时,我创建了几个实体,我坚持通过核心数据框架。没问题。
当我想根据用户在UIToolbar
内选择的标签显示项目列表时,问题就出现了。我已经设置了参数来显示每个启动的sql请求(-com.apple.CoreData.SQLDebug 1
),真正尴尬的是,当我通过sqlite从核心数据访问我的数据库时,控制台中显示的查询给我结果,不,0行返回...
有人遇到过类似的问题吗?
以下是用于检索项目列表的代码:
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];
此代码生成此sql请求(在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 = ?)
答案 0 :(得分:2)
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"ANY myEmployees.employeeNumber = %d AND myProjectType.projectTypeName = %d", [theEmployee valueForKey:@"employeeNumber"], [theProjType valueForKey:@"projectTypeName"]];
你确定这是对的吗? valueForKey:始终返回NSObject,但占位符为%d。这应该会产生奇怪的行为。试试%@而不是