0

I'm trying to create a data storage for my application using CoreData. From what I know, Xcode 8 CoreData is using persistentContainer instead of managedObjectContext.

I've created a data model with my required entities and created an NSManagedObject subclass from the Editor menu.

My problem is that when I want to use the persistentContainer, there is no identifier found.

#import "UserCredentials+CoreDataClass.h"

//Fetch all username to array
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"UserCredentials"];
NSError *requestError = nil;


//I couldn't find the persistent container even though I had imported my header file.
NSArray *usernames = [self.persistentContainer.viewContext executeFetchRequest:fetchRequest error:&requestError];

I realised that my CoreDataClass did not even have the property persistentContainer at all. Where can I declare this at, so I can access my data storage?

iamhx
  • 472
  • 6
  • 24
  • Use `self.context executeFetchRequest:fetchRequest error:&requestError];`. `NSManagedObjectContext` is not entirely removed, every `NSManagedObject` should exist with here. – Sachin Vas Feb 15 '17 at 08:01
  • @Ludovic, I am using Objective C. – iamhx Feb 15 '17 at 08:02
  • @iamhx Not a real answer but you should read this (it"s swift but easy to convert to ObjC) : http://stackoverflow.com/questions/37956720/how-to-create-managedobjectcontext-using-swift-3-in-xcode-8 – Ludovic Feb 15 '17 at 08:02
  • @iamhx Yes I deleted my comment, sorry but it's the way you should implement persistentContainer – Ludovic Feb 15 '17 at 08:03
  • @Ludovic no worries. Let me try to read the swift code and see whether I can convert this... – iamhx Feb 15 '17 at 08:04
  • Inder Kumar Rathore did this for you! :) – Ludovic Feb 15 '17 at 08:13

2 Answers2

1

I am assuming you have selected core data option while creating your object. Your object context is null because it is store into AppDelegate. So you need to get context reference from appdelegate like below.

NSManagedObjectContext *context = ((AppDelegate*)[[UIApplication sharedApplication] delegate]).persistentContainer.viewContext;
NSArray *usernames = [context executeFetchRequest:fetchRequest error:&requestError];
Chandan kumar
  • 1,074
  • 12
  • 31
Ratnesh Shukla
  • 1,128
  • 13
  • 24
  • I did not select the core data option as my project is an existing one – iamhx Feb 15 '17 at 08:13
  • 1
    So Just create a New project and copy CoreData code from AppDelegate and add into your exiting project. I am saying because it is easy way to avoid error. Otherwise show your all code used for CoreData. – Chandan kumar Feb 15 '17 at 08:17
  • cool! That worked. Now I am trying to get context reference from AppDelegate from your provided answer, however your first line seems to have some syntax error. – iamhx Feb 15 '17 at 08:30
  • Solved :) Must import AppDelegate header file. Thanks! – iamhx Feb 15 '17 at 08:47
1

You should create property

//.h file

@property (readonly, strong) NSPersistentContainer *persistentContainer;

//.m file

@synthesize persistentContainer = _persistentContainer;

- (NSPersistentContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"CoreDataModel"]; //e.g. CoreDataModel.xcdatamodeld
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

                    /*
                     Typical reasons for an error here include:
                     * The parent directory does not exist, cannot be created, or disallows writing.
                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                     * The device is out of space.
                     * The store could not be migrated to the current model version.
                     Check the error message to determine what the actual problem was.
                    */
                    RLog(@"Unresolved error %@, %@", error, error.userInfo);
                    abort();
                }
            }];
        }
    }

    return _persistentContainer;
}
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184