Core Data Error --- NSManagedObjectModel issue - ios

I have set up the core data stack and i tried to test and build the project. However, i received the error as:
After I did the research, the potential error could be: the file's model file's name is different with the name in the Core Data Stack. But it still crashes as before.
#pragma mark - Core Data stack
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"RunMaster" withExtension:#"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"RunMaster.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

Do you have made change on data structure ? new scheme ? entity ?
for quick test uninstall application and install it again if that work again
then you should implement the update of your data struct or change the storage name / migrate data...
Your error suggest a nil URL in modelURL

Related

iOS - This NSPersistentStoreCoordinator has no persistent stores (corrupt file).

I am using CoreData in my iOS app to allow the user to save some data to his phone, and whenever I make an update, I am getting only one or two users that have the following crash message:
Fatal Exception: NSInternalInconsistencyException
This NSPersistentStoreCoordinator has no persistent stores (corrupt file). It cannot perform a save operation.
Multiple times though, which indicates that they simply can't open the app anymore, it crashes on every relaunch. This only happens to 1, maximum 2 users out of a couple of thousand, and it's impossible for me to replicate this.
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"modelName" withExtension:#"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"app name.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = #{
NSMigratePersistentStoresAutomaticallyOption : #YES,
NSInferMappingModelAutomaticallyOption : #YES,
NSSQLiteManualVacuumOption : #YES
};
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
}
return _persistentStoreCoordinator;
}
This is the CoreData stack in my AppDelegate.
What am I doing wrong?

setPersistentStoreCoordinator Core Data BAD ACCESS

I had code that worked before IOS 9. After I compiled with some other changes the to code now the line in the code below dies with a BAD ACCESS message. I cant for the life of me understand why this used to work and now it doesnt.
[_managedObjectContext setPersistentStoreCoordinator:coordinator];<<< THIS LINE CRASHES - BAD ACCESS
#pragma mark - Core Data stack
- (void)setmanagedObjectContext
{
if (_managedObjectContext != nil) {
return ;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
_managedObjectContext = [NSManagedObjectContext alloc];
**[_managedObjectContext setPersistentStoreCoordinator:coordinator];<<< THIS LINE CRASHES - BAD ACCESS**
}
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"wrh" withExtension:#"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"wrh.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
I rewrote the above with a hint from Willeke. I looked up his hint and figured a better way to do this. Here is the code now that works!
- (void)setmanagedObjectContext
{
if (_managedObjectContext != nil) {
return ;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"wrl.sqlite"];
_managedObjectContext =
[[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
self.managedObjectContext.persistentStoreCoordinator =
[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
NSError* error;
[self.managedObjectContext.persistentStoreCoordinator
addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:nil
error:&error];
if (error) {
NSLog(#"error: %#", error);
}
self.managedObjectContext.undoManager = [[NSUndoManager alloc] init];
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"wrh" withExtension:#"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}

iOS 8 CoreData Issue: recordChangeSnapshot:forObjectID:: global ID may not be temporary when recording

I'm migrating my App from iOS 7 to iOS 8, and I've been receiving the following error in Xcode when I try to save the Core Data context. This error was not present in iOS 7 and Xcode 5. it throws exception at below line
NSError * saveError = nil;
if(![self.managedObjectContext save:&saveError])
{ // crash on save context line.
//error message.
}
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'recordChangeSnapshot:forObjectID:: global ID may not be temporary when recording.
I am sinking data on main thread and trying to save it in core data .
Below is code for managed object context in app delegate class :
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"MCRM" withExtension:#"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"MCRM.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
and it crash in data manager class :
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
}
Code work fine in iOS7 and Xcode5.1 .
I have already tried with thread confinement (-[NSManagedObjectContext init]) to replace with [NSManagedObjectContext initWithConcurrencyType:] to NSConfinementConcurrencyType and NSMainQueueConcurrencyType with performBlockWithWait^{} code injection but still facing issue .

NSInternalInconsistencyException: Cannot get NSPersistentStore to initiate or create a store

I've been working all day to solve an error that I've found to be an NSInternalInconsistencyException error. The error console message I get is
"This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation." Also I get "Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x1)."
I have the following code in my App Delegate:
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
}
#pragma mark Core Data stack
- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return _managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
_managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"shindy.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
#pragma mark Application's documents directory
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
I have throughly read up on other questions similar to this one, and it appears that my code is consistent, and I shouldn't be having this problem. And yet, I am...
I have tried both cleaning & deleting the app and reinstalling. I tried the app on both a device & on the simulator. I changed the name of the appended URLPath, "shindy.sqlite", to various names I haven't used before. I also tried forcefully instantiating [self saveContext]; in my applicationDidFinishLaunching method in an attempt to make my app create the store.
All of my attempts to solve the problem have simply returned the same error. I don't know what else to try.
I eventually figured out what I was doing wrong. Unfortunately, it probably won't be of much help to most of you as it was a silly error that I made.
I was doing the normal "appending" onto the URL in the App Delegate. And then "appending" again in the first view controller that would load. This resulted in my code appending a name onto an already created sqlite folder.

NSManagedObjectModel mergedModelFromBundles error

I'm working with Core Data and having a lot of trouble's getting data into the database right off the start of my app.
Below is some of the code I've grabbed from a tutorial I followed.
The point where I get the SIGABRT is outlined below.
Any suggestions or help is appreciated
Thanks
//THIS FUNCTION IS CALLED AFTER MY APP DID FINISHING LOADING IN THE
// IN THE APP DELEGATE
-(void)loadData
{
NSManagedObjectContext *context = [self managedObjectContext];
NewModel *newModel = (NewModel *)[NSEntityDescription insertNewObjectForEntityForName:#"NewModel" inManagedObjectContext:context];
//ADD MORE DATA TO ENTITY
}
/**
Returns the managed object context for the application.
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
*/
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
else
{
NSLog(#"Error");
}
return managedObjectContext;
}
/**
Returns the managed object model for the application.
If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
*/
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
/**********************************************************/
// SIGABRT HAPPENS IN THE NEXT LINE OF CODE
/**********************************************************/
managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return managedObjectModel;
}
/**
Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSString *storePath = [ [self applicationDocumentsDirectory] stringByAppendingPathComponent:#"NewModel.db"];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:#"LeagueModel" ofType:#"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
/**
Returns the path to the application's Documents directory.
*/
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
if you check the apple's documentation:
mergedModelFromBundles:
Returns a model created by merging all the models found in given bundles.
+ (NSManagedObjectModel *)mergedModelFromBundles:(NSArray *)bundles
Parameters
***bundles***
An array of instances of NSBundle to search. If you specify nil, then the main bundle is searched.
***Return Value***
A model created by merging all the models found in bundles.
you are passing nil where you need to pass the array for bundles this is the reason for crash.

Resources