I have a model class which implements the NSCoding protocol. I have a property called dataModel which is an instance of this class. When I save/load this instance to/from disk, should I use the synthesized accessors for dataModel or the ivars directly? And why?
This is under iOS 5 and ARC, and my property is declared as:
@property (strong, atomic) QardDataModel *dataModel;
For illustration this would be the accessor way of doing it:
-(void)saveData {
[NSKeyedArchiver archiveRootObject:self.dataModel toFile:[self saveFilePath]];
}
-(void)loadData {
self.dataModel = [NSKeyedUnarchiver unarchiveObjectWithFile:[self saveFilePath]];
}
There is a convenience method called saveFilePath which returns the path of the archived file.
If you are archiving/unarchiving a data set with millions of objects, then there is a very slight performance benefit to accessing the variable directly.
But if you have that much data, usually it will not fit in available RAM anyway, so you should be using SQLite or Core Data.
If however, you are repeatedly archiving and unarchiving lots of small objects (hundreds of thousands of times), then you should use Instruments to check if
objc_msg_sendis a performance drag. Accessing variables directly is the way to fix that.Aside from that, it’s generally better to use the accessor methods, for a variety of reasons (none of them particularly important).