Archiving and Unarchiving DataModel Object


/ Published in: Objective C
Save to your folder(s)

This is a follow up to another snippet which shows how to create the object you want to archive and unarchive.


Copy this code and paste it in your HTML
  1. /* INTERFACE */
  2.  
  3. #import <Foundation/Foundation.h>
  4.  
  5. #define kFilename @"dataarchive.plist"
  6. #define kDataKey @"Data"
  7.  
  8. @class DataModel;
  9. @interface DataManager : NSObject {
  10.  
  11. DataModel *dataModel;
  12. }
  13.  
  14. @property (nonatomic, retain) DataModel *dataModel;
  15.  
  16. -(NSString *)dataFilePath;
  17. -(void)initDataModel;
  18. -(void)applicationWillTerminate:(NSNotification *)notification;
  19.  
  20. +(DataManager *)sharedDataManager;
  21.  
  22. @end
  23.  
  24. /* IMPLEMENTATION */
  25.  
  26. #import "DataManager.h"
  27. #import "SynthesizeSingleton.h"
  28. #import "DataModel.h"
  29. #import "FaveVO.h"
  30.  
  31. @implementation DataManager
  32.  
  33. SYNTHESIZE_SINGLETON_FOR_CLASS(DataManager);
  34.  
  35. @synthesize dataModel;
  36.  
  37. //-----------------------------------------------------------------------------------------------------
  38. - (NSString *)dataFilePath
  39. {
  40. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  41. NSString *documentsDirectory = [paths objectAtIndex:0];
  42. return [documentsDirectory stringByAppendingPathComponent:kFilename];
  43. }
  44. //-----------------------------------------------------------------------------------------------------
  45. -(void)initDataModel
  46. {
  47. NSString *filePath = [self dataFilePath];
  48.  
  49. if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
  50. {
  51. NSLog(@"FILE EXISTS");
  52.  
  53. NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
  54. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  55.  
  56. DataModel *decodedDataModel = [unarchiver decodeObjectForKey:kDataKey];
  57.  
  58. dataModel = [[DataModel alloc] init];
  59.  
  60. NSEnumerator *enumerator;
  61. enumerator = [decodedDataModel.favesArray objectEnumerator];
  62.  
  63. FaveVO *decodedFaveVO;
  64.  
  65. while (decodedFaveVO = [enumerator nextObject])
  66. {
  67. [dataModel createNewFaveVOFromFaveVO:decodedFaveVO];
  68. }
  69.  
  70. int index = decodedDataModel.faveIndex;
  71. dataModel.faveIndex = index;
  72. dataModel.currentFave = [dataModel.favesArray objectAtIndex:index];
  73.  
  74. [unarchiver finishDecoding];
  75. [unarchiver release];
  76. [data release];
  77. }
  78. else
  79. {
  80. NSLog(@"FILE DOES NOT EXIST");
  81.  
  82. dataModel = [[DataModel alloc] init];
  83. }
  84.  
  85. UIApplication *app = [UIApplication sharedApplication];
  86. [[NSNotificationCenter defaultCenter] addObserver:self
  87. selector:@selector(applicationWillTerminate:)
  88. name:UIApplicationWillTerminateNotification
  89. object:app];
  90.  
  91. }
  92. //-------------------------------------------------------------
  93. - (void)applicationWillTerminate:(NSNotification *)notification
  94. {
  95. NSLog(@"Application will terminate");
  96.  
  97. NSMutableData *data = [[NSMutableData alloc] init];
  98. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  99.  
  100. [archiver encodeObject:dataModel forKey:kDataKey];
  101. [archiver finishEncoding];
  102. [data writeToFile:[self dataFilePath] atomically:YES];
  103.  
  104. [archiver release];
  105. [data release];
  106. }
  107.  
  108.  
  109. //--------------------------------------------------
  110. -(void)dealloc
  111. {
  112. [dataModel release];
  113. [super dealloc];
  114. }
  115.  
  116.  
  117. @end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.