title: Store and fetch NSMutableArray in Core Data
body: <p>I would like to save the <code>NSMutableArray</code> to <code>Core-Data</code> when my application terminates/goes into background and I would like to load the <code>NSMutableArray</code> when my application launches/becomes active.</p>
  
  <p>I don't have a very good understanding of <code>Core-Data</code> yet. This is my first time working with it. I've looked at a bunch of videos, tutorials, previous Stackoverflow questions and Apple's documentation. I think what I am trying to do falls under the Non-Standard Persistent Attributes chapter in Apple's <code>Core-Data</code> documentation. </p>
  
  <p>I have set up an Entity called TableViewList and I have given it an attribute called List of type transformable.</p>
  
  <p>Here is my AppDelegate.h and .m code. All advice would be wonderful. </p>
  
  <h1>AppDelegate.h</h1>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import "TableViewController.h"
  
  @interface AppDelegate : UIResponder &lt;UIApplicationDelegate&gt;
  
  @property (strong, nonatomic) UIWindow *window;
  
  @property(nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
  @property(nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
  @property(nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
  
  -(NSString *) applicationDocumentsDirectory;    
  @end
  </code></pre>
  
  <h1>AppDelegate.m</h1>
  
  <pre><code>#import &lt;CoreData/CoreData.h&gt;
  #import "AppDelegate.h"
  #import &lt;UIKit/UIKit.h&gt;
  
  @interface AppDelegate ()
  
  @end
  
  @implementation AppDelegate
  @synthesize managedObjectModel;
  @synthesize managedObjectContext;
  @synthesize persistentStoreCoordinator;
  - (void)applicationDidEnterBackground:(UIApplication *)application {
  
      AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
  
      NSManagedObjectContext *context = [appDelegate managedObjectContext];
      NSManagedObject *newContact;
      newContact = [NSEntityDescription insertNewObjectForEntityForName:@"TableViewList" inManagedObjectContext:context];
      NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:ListArray];        
      [newContact setValue:arrayData forKey:@"list"];
      NSError *error = nil;        
      [context save:&amp;error];
  
  
  }
  
  - (void)applicationDidBecomeActive:(UIApplication *)application {
      NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:[NSBundle allBundles]];
      NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
      NSURL *url = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent: @"App1.sqlite"];
      [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:nil];
      managedObjectContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
      managedObjectContext.persistentStoreCoordinator = coordinator;
  
  
      NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
  
      NSEntityDescription *entity = [NSEntityDescription entityForName:@"TableViewList" inManagedObjectContext:self.managedObjectContext];
      [fetchRequest setEntity:entity];
      NSError *error = nil;
      NSArray *result = [self.managedObjectContext executeFetchRequest:fetchRequest error:&amp;error];
  
      if (error) {
          NSLog(@"unable to execute fetch request");
          NSLog(@"%@, %@", error, error.localizedDescription);
      }
      else
          NSLog(@"%@",result);
  }
  </code></pre>
  
  <p>The result array returns an empty array. I don't think I'm saving and fetching the array correctly. Thanks in advance for your help!</p>
  
  <p>I used <a href="http://blog.soff.es/archiving-objective-c-objects-with-nscoding" rel="nofollow">this link</a> to implement <code>NSCoding</code> in my object. </p>