as we know NSFetchRequest's propertiesToFetch can limite the properties we want, it can reduce the footprint of memory. If we do like this:
request.propertiesToFetch = [NSArray arrayWithObjects:@"nID", nil];
and:
NSString *strID = [NSString stringWithFormat:@"%@", aPerson.nID];
cell.textLabel.text = strID;
then debug output(and not fire fault):
CoreData: sql: SELECT t0.Z_ENT, t0.Z_PK, t0.ZNID FROM ZPERSON t0 ORDER BY t0.ZNID
A strange thing happened, however, when i add a new unmodeled property to the subclass of NSManagedObject Person. Just like this:
@interface Person (Ex)
@property (nonatomic, retain) NSString *strTempName;
@end
@implementation Person (Ex)
@dynamic strTempName;
-(void)setStrTempName:(NSString *)strTempName
{
[self setPrimitiveValue:strTempName forKey:@"strTempName"];
}
-(NSString*)strTempName
{
return [self primitiveValueForKey:@"strTempName"];
}
And this is where the new unmodeled property is accessed:
NSString *strTT = aPerson.strTempName;
then debug output:
2013-05-06 10:43:07.789 TestCoreData[988:c07] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZBISFORTEST, t0.ZBISINTRASH, t0.ZNID, t0.ZSTRNAME, t0.ZSTROK, t0.ZSTROK2, t0.ZSTRTEST1, t0.ZSTRTEST2, t0.ZADEPART, t0.ZAMIDDLETHUMNAIL, t0.ZANORMALPIC, t0.ZASMALLTHUMNAIL FROM ZPERSON t0 WHERE t0.Z_PK = ?
2013-05-06 10:43:07.790 TestCoreData[988:c07] CoreData: annotation: sql connection fetch time: 0.0010s
2013-05-06 10:43:07.791 TestCoreData[988:c07] CoreData: annotation: total fetch execution time: 0.0018s for 1 rows.
2013-05-06 10:43:07.792 TestCoreData[988:c07] CoreData: annotation: fault fulfilled from database for : 0x898a030 <x-coredata://CAD35D43-F6B2-4463-B59B-C9A3CD488935/Person/p51846>
From the above output message, we can find the unmodeled property caused that instead of SELECT t0.Z_ENT, t0.Z_PK, t0.ZNID, SELECT all of properties sentence is generated and fault is fired!
however, I have read some messages about unmodeled property(here is link):
Because unmodeled properties are only attributes of the custom NSManagedObject subclass and not the entity, the fault objects know nothing about them. Fault objects are initialized from the data model so that all the keys they respond to must be in the data model. This means faults will not reliably respond to request for unmodeled properties.
why did the strange thing happen?
thanks in advance.
@Anonymous,
choice 1): app crashed and the debug output showed:
2013-05-06 14:03:05.665 TestCoreData[1794:c07] -[Person strTempName]: unrecognized selector sent to instance 0x6ba77b0
2013-05-06 14:03:30.395 TestCoreData[1794:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Person strTempName]: unrecognized selector sent to instance 0x6ba77b0'
choice 2): at first, the methods like willAccessValueForKey etc are to send KVO change notification, just like comments in NSManagedObject.h:
- (void)willAccessValueForKey:(NSString *)key; // read notification
- (void)didAccessValueForKey:(NSString *)key; // read notification (together with willAccessValueForKey used to maintain inverse relationships, to fire faults, etc.)
secondly, following choice 2) code, the strange thing(to fire faults) is still alive in my app.
also thanks to your reply.