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.nameis definitely set, I NSLog it right before the line of predicate -
And 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
BankDetailstoBankDetailin .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
BankDetailsshows 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 oldBankDetails. 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!