title: Core-Data : Data is not been saved up
body: <p>while following the ebook of <a href="http://timroadley.com/" rel="nofollow">http://timroadley.com/</a> i am inserting. but when i check in sqlite their is no data present in it.also i have used -com.apple.CoreData.SQLDebug to debug but no query is being shown.<a href="https://drive.google.com/file/d/0B5T2z28ShqrFWlAwZE9lRFhlOU0/edit?usp=sharing" rel="nofollow">Source Code</a></p>
  
  <p><strong>Solution</strong>:- Data will only show in sqlite when i will terminate the app or the app will go in background after pressing home button.</p>
  
  <p><strong>AppDelegate.h</strong></p>
  
  <pre><code>    #import &lt;UIKit/UIKit.h&gt;
      #import "CoreDataHelper.h"
      #import &lt;CoreData/CoreData.h&gt;
      @interface AppDelegate : UIResponder &lt;UIApplicationDelegate&gt;
      @property (strong, nonatomic) UIWindow *window;
      @property (nonatomic, strong, readonly) CoreDataHelper *coreDataHelper;
      @end
  </code></pre>
  
  <p><strong>AppDelegate.m</strong></p>
  
  <pre><code>- (void)demo {
  
      NSArray *newItemNames = [NSArray arrayWithObjects:
                               @"Apples", @"Milk", @"Bread", @"Cheese", @"Sausages", @"Butter", @"Orange Juice", @"Cereal", @"Coffee", @"Eggs", @"Tomatoes", @"Fish", nil];
      for (NSString *newItemName in newItemNames) {
          Item *newItem =
          [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:_coreDataHelper.context];
          newItem.name = newItemName;
          NSLog(@"Inserted New Managed Object for '%@'", newItem.name);
      }
  }
  
  - (CoreDataHelper*)cdh {
      if (!_coreDataHelper) {
          _coreDataHelper = [CoreDataHelper new];
          [_coreDataHelper setupCoreData];
      }
      return _coreDataHelper;
  }
  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  {
      return YES;
  }
  - (void)applicationDidEnterBackground:(UIApplication *)application
  {
      [[self cdh] saveContext];
  }
  - (void)applicationDidBecomeActive:(UIApplication *)application
  {
      [self cdh];
      [self demo];
  }
  
  - (void)applicationWillTerminate:(UIApplication *)application
  {
      [[self cdh] saveContext];
  }
  </code></pre>
  
  <p><strong>CoreDataHelper.h</strong></p>
  
  <pre><code>@property(nonatomic,readonly) NSManagedObjectContext *context;
  @property(nonatomic,readonly) NSManagedObjectModel *model;
  @property(nonatomic,readonly) NSPersistentStore *store;
  @property(nonatomic,readonly) NSPersistentStoreCoordinator *coordinator;
  
  -(void)setupCoreData;
  -(void)saveContext;
  </code></pre>
  
  <p><strong>CoreDataHelper.m</strong></p>
  
  <pre><code>#pragma mark - FILES
      NSString *storeFilename = @"Grocery-Dude.sqlite";
  
      #pragma mark - PATHS
      - (NSString *)applicationDocumentsDirectory {
           return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];
      }
      - (NSURL *)applicationStoresDirectory {
          NSURL *storesDirectory =
          [[NSURL fileURLWithPath:[self applicationDocumentsDirectory]]
           URLByAppendingPathComponent:@"Stores"];
  
          NSFileManager *fileManager = [NSFileManager defaultManager];
          if (![fileManager fileExistsAtPath:[storesDirectory path]]) {
              NSError *error = nil;
              if ([fileManager createDirectoryAtURL:storesDirectory
                        withIntermediateDirectories:YES
                                         attributes:nil
                                              error:&amp;error]) {
                          }
              else {
                  NSLog(@"FAILED to create Stores directory: %@", error);}
          }
          return storesDirectory;
      }
      - (NSURL *)storeURL {
          return [[self applicationStoresDirectory]
                  URLByAppendingPathComponent:storeFilename];
      }
  
      #pragma mark - SETUP
      - (id)init {
          self = [super init];
          if (!self) {return nil;}
          _model = [NSManagedObjectModel mergedModelFromBundles:nil];
          _coordinator = [[NSPersistentStoreCoordinator alloc]
                          initWithManagedObjectModel:_model];
          _context = [[NSManagedObjectContext alloc]
                      initWithConcurrencyType:NSMainQueueConcurrencyType];
          [_context setPersistentStoreCoordinator:_coordinator];
          return self;
      }
      - (void)loadStore {
          if (_store) {return;} // Don’t load store if it's already loaded
          NSDictionary *options =
          @{NSSQLitePragmasOption: @{@"journal_mode": @"DELETE"}};
  
          NSError *error = nil;
          _store = [_coordinator addPersistentStoreWithType:NSSQLiteStoreType
                                              configuration:nil
                                                        URL:[self storeURL]
                                                    options:options error:&amp;error];
          if (!_store) {NSLog(@"Failed to add store. Error: %@", error);abort();}
          else         {NSLog(@"Successfully added store: %@", _store);}
      }
      - (void)setupCoreData {
          [self loadStore];
      }
  
      #pragma mark - SAVING
      - (void)saveContext {
  
          if ([_context hasChanges]) {
              NSError *error = nil;
              if ([_context save:&amp;error]) {
                  NSLog(@"_context SAVED changes to persistent store");
              } else {
                  NSLog(@"Failed to save _context: %@", error);
              }
          } else {
              NSLog(@"SKIPPED _context save, there are no changes!");
          }
      }
  </code></pre>