Revision: 26285
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at April 22, 2010 11:34 by LeeProbert
Initial Code
/* INTERFACE */ #import <Foundation/Foundation.h> #define kFilename @"dataarchive.plist" #define kDataKey @"Data" @class DataModel; @interface DataManager : NSObject { DataModel *dataModel; } @property (nonatomic, retain) DataModel *dataModel; -(NSString *)dataFilePath; -(void)initDataModel; -(void)applicationWillTerminate:(NSNotification *)notification; +(DataManager *)sharedDataManager; @end /* IMPLEMENTATION */ #import "DataManager.h" #import "SynthesizeSingleton.h" #import "DataModel.h" #import "FaveVO.h" @implementation DataManager SYNTHESIZE_SINGLETON_FOR_CLASS(DataManager); @synthesize dataModel; //----------------------------------------------------------------------------------------------------- - (NSString *)dataFilePath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return [documentsDirectory stringByAppendingPathComponent:kFilename]; } //----------------------------------------------------------------------------------------------------- -(void)initDataModel { NSString *filePath = [self dataFilePath]; if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { NSLog(@"FILE EXISTS"); NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; DataModel *decodedDataModel = [unarchiver decodeObjectForKey:kDataKey]; dataModel = [[DataModel alloc] init]; NSEnumerator *enumerator; enumerator = [decodedDataModel.favesArray objectEnumerator]; FaveVO *decodedFaveVO; while (decodedFaveVO = [enumerator nextObject]) { [dataModel createNewFaveVOFromFaveVO:decodedFaveVO]; } int index = decodedDataModel.faveIndex; dataModel.faveIndex = index; dataModel.currentFave = [dataModel.favesArray objectAtIndex:index]; [unarchiver finishDecoding]; [unarchiver release]; [data release]; } else { NSLog(@"FILE DOES NOT EXIST"); dataModel = [[DataModel alloc] init]; } UIApplication *app = [UIApplication sharedApplication]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app]; } //------------------------------------------------------------- - (void)applicationWillTerminate:(NSNotification *)notification { NSLog(@"Application will terminate"); NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:dataModel forKey:kDataKey]; [archiver finishEncoding]; [data writeToFile:[self dataFilePath] atomically:YES]; [archiver release]; [data release]; } //-------------------------------------------------- -(void)dealloc { [dataModel release]; [super dealloc]; } @end
Initial URL
Initial Description
This is a follow up to another snippet which shows how to create the object you want to archive and unarchive.
Initial Title
Archiving and Unarchiving DataModel Object
Initial Tags
Initial Language
Objective C