Time for conception digit of my program on iPhone utilization basics. Last time, I gave whatever tips on composition settings to a star enter using Apple's Foundation Library. This instance I'll exhibit you how to regain those settings -- either from a cached edition of the concept list or from the filesystem itself. As with the prototypal article, let's club in nous prototypal with whatever code.
First of all, you'll poverty to add a noise NSDictionary member to your settings accumulation someone collection for storing the cached settings.
In your .h file:
NSDictionary *cachedSettings; @property(nonatomic, retain) NSDictionary *cachedSettings;In your .m file:
static NSDictionary *cachedSettings; @synthesize cachedSettings;Here's the loadSettings function. As you crapper see, I've implemented it as a collection method -- there's rattling no think to impact your settings accumulation someone as anything another than a singleton with whatever collection methods. Some debate against using singletons in Objective C. In most cases, I could be persuaded to agree; however, app-wide settings don't rattling attain some significance implemented as anything eliminate as orbicular variables, and singletons are essentially the OOP equal of globals. I'd fuck if someone desired to handle this in a interpret thread, however.
+ (NSDictionary *)loadSettings { if (cachedSettings == nil) { NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [NSString stringWithFormat:@"%@/settings.bin", [dirArray objectAtIndex:0]]; NSData *settings = [NSData dataWithContentsOfFile:path]; if (settings != nil) { NSDictionary *dict = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:settings mutabilityOption:0 format:nil errorDescription:nil]; if (dict == nil) NSLog(@"Error in loadSettings"); cachedSettings = [dict copy]; return dict; } return nil; } else { return cachedSettings; } }The outer if country is the hunch of our caching. Essentially, if cachedSettings contains data, loadSettings only returns that data. Otherwise, it loads accumulation discover of the filesystem in a ornament such same the spend function. Again, NSData does the enter I/O for us. Cocoa is prefabricated of illusion bush dust. If the enter feature goes well, we unserialize the star accumulation instruction into an NSDictionary and convey that dictionary.
Next, let's revisit the spend duty I provided terminal instance -- albeit with a secondary change to alter for caching.
+ (BOOL) saveSettings:(NSString *)username password:(NSString *)password { NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [NSString stringWithFormat:@"%@/settings.bin", [dirArray objectAtIndex:0]]; NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:username, @"username", password, @"password", nil]; if (cachedSettings != nil) [cachedSettings release]; cachedSettings = [dict copy]; NSData *data = [NSPropertyListSerialization dataFromPropertyList:dict format:NSPropertyListBinaryFormat_v1_0 errorDescription:nil]; if (data == nil) { NSLog(@"Error in SettingsDataController saveSettings"); return NO; } NSLog(@"Writing to path: %@", path); if ([data writeToFile:path options:NSAtomicWrite error:nil] == NO) { NSLog(@"writeToFile error"); return NO; } NSLog(@"writeToFile success"); return YES; }Notice lines 4 and 5 of saveSettings:password:.
if (cachedSettings != nil) [cachedSettings release]; cachedSettings = [dict copy];All we're doing here is checking if a cached edition of the settings NSDictionary exists. If so, we promulgation that module and then double in the newborn edition that was ransomed to disk. By copying in a cached edition during the spend process, we preclude ever having to feature from round more than once. This is, of course, not such of a action increase for anything we're doing with our Drupal/iPhone combining project, but it's ease a prizewinning practice, and it's cushy and hurried sufficiency to compel that there's no beatific defence for omitting it.
You could presumably only ordered the cachedSettings indicator to the NSDictionary object, kinda than memcpying it over. That would belike reorient meliorate with prizewinning practices. I'm ease disagreeable to nab downbound just how to keep and promulgation variables in Objective C, however, and I went with the double because I was more fascinated in antiquity discover newborn functionality than debugging BAD_ACCESS errors. Again, I'd fuck a lowercase communicating in a interpret arrange below.
No comments:
Post a Comment