核心数据:为什么我不能获取任何东西与我的谓词 [英] Core Data: why I can't fetch anything with my predicate
问题描述
代码有什么问题吗?我可以使用这个谓词没有任何东西。注释掉谓词,然后我可以从实体BankDetail获取所有对象。所以我认为问题存在于这两行。
// self.bankInfo.name在prepareForSegue中设置在第一个视图控制器
NSPredicate * predicate = [NSPredicate predicateWithFormat:@info.name =%@,self.bankInfo.name];
[request setPredicate:predicate];
我的模型包含两个实体,它们是一对一关系
BankInfo.h
@class BankDetail;
@interface BankInfo:NSManagedObject
@property(nonatomic,retain)NSString * city;
@property(nonatomic,retain)NSString * name;
@property(nonatomic,retain)NSString * state;
@property(nonatomic,retain)BankDetail * detail;
@end
BankDetail.h
@class BankInfo;
@interface BankDetail:NSManagedObject
@property(nonatomic,retain)NSString * closeDate;
@property(nonatomic,retain)NSString * updateDate;
@property(nonatomic,retain)NSString * zip;
@property(nonatomic,retain)NSString * acquireInstitution;
@property(nonatomic,retain)BankInfo * info;
@end
EDIT: p>
提供更多详情:
-
.bankInfo.name
是绝对设置的,我NSLog正好在谓词之前 -
$ c> viewDidLoad :
NSEntityDescription * entity = [NSEntityDescription entityForName:@BankDetailinManagedObjectContext:上下文];
NSLog(@[entity description] is%@,[entity description]);
info =(),name信息,isOptional 1,isTransient 0,实体BankDetail,renamingIdentifier信息,验证谓词\ n),warningHashModifier(null)\\\
userInfo {\ n},目标实体BankInfo,inverseRelationship细节,minCount 1,maxCount 1,isOrdered 0,deleteRule 2;
EDIT2:
出现与谓词。该错误是由其他地方的粗心错误引起的(见接受的答案,它是关于重命名)。如果您对谓词有疑问,请忽略此信息。
我终于修复了错误。它不是关于谓词。我在Xcode编辑器中的.xcdatamodeld中将实体从 BankDetails
重命名为 BankDetail
重命名任何其他文件。
然后我手动重命名自动生成的NSManagedObject子类文件中的各个部分,以及引用它的其他类文件,直到所有警告消失。我想我已经重命名所有必要的,但我不是。程序不运行,因为我希望我在问题中描述,但没有错误,没有警告,Xcode只是编译和运行。
一段时间后,当我终于尝试重新生成子类文件来修复错误时,旧的 BankDetails
作为类名称,并出现了大量的命名错误。我认为这是Xcode的一些bug在第一。所以我清理生成和重新生成子类文件。然而,它仍然是好的旧 BankDetails
。经过几次尝试,我发现数据模型检查器中的问题(见下面的屏幕截图)。
道德:总是重新命名。 =https://i.stack.imgur.com/a2fzl.pngalt =输入图片说明here>>
Is there something wrong with the code? I can fetch nothing using this predicate. Comment out the predicate then I can fetch all objects from entity "BankDetail". So I think problem resides in these two lines.
// self.bankInfo.name is set in prepareForSegue in first view controller
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"info.name = %@",self.bankInfo.name];
[request setPredicate:predicate];
My model includes two entities, which are in one-to-one relationship
BankInfo.h
@class BankDetail;
@interface BankInfo : NSManagedObject
@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * state;
@property (nonatomic, retain) BankDetail * detail;
@end
BankDetail.h
@class BankInfo;
@interface BankDetail : NSManagedObject
@property (nonatomic, retain) NSString * closeDate;
@property (nonatomic, retain) NSString * updateDate;
@property (nonatomic, retain) NSString * zip;
@property (nonatomic, retain) NSString * acquiringInstitution;
@property (nonatomic, retain) BankInfo * info;
@end
EDIT:
To provide more detail:
self.bankInfo.name
is definitely set, I NSLog it right before the line of predicateAnd I do this in
viewDidLoad
:NSEntityDescription *entity = [NSEntityDescription entityForName:@"BankDetail" inManagedObjectContext:context]; NSLog(@"[entity description] is %@",[entity description]);
Get this in console:
info = "(<NSRelationshipDescription: 0x6d3eb30>), name info, isOptional 1, isTransient 0, entity BankDetail, renamingIdentifier info, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, destination entity BankInfo, inverseRelationship detail, minCount 1, maxCount 1, isOrdered 0, deleteRule 2";
EDIT2: Turn out there's nothing wrong with the predicate. The bug is caused by a careless mistake elsewhere (see the accepted answer, it's about renaming). Please IGNORE this post if you have question about predicate.
I finally fix the bug. It's not about the predicate at all. I rename the entity from BankDetails
to BankDetail
in .xcdatamodeld in Xcode editor by simply hit return key and change it, like renaming any other file.
Then I go ahead to manually rename various parts in the auto generated NSManagedObject subclass files, and in other class files that reference it, until all warnings go away. I think I have renamed all that are necessary, but I wasn't. The program doesn't run as I expect like I described in the question, but no error, no warning, Xcode just compile and run.
After some time, when I finally try to regenerate the subclass files to fix the bug, the old BankDetails
shows up as class name, and lots of naming errors come out. I thought it was some bug of Xcode at first. So I clean the build and regenerate subclass files again. Yet, it's still the good old BankDetails
. After a few attempts, I found the problem in Data Model Inspector (see screenshot below). Change the Class name and everything runs perfectly.
Moral: always rename in extreme caution!
这篇关于核心数据:为什么我不能获取任何东西与我的谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!