awakeFromInsert never called - iphone

I have an application that uses Core Data. I have two data models in the project and I create the ManagedObjectContext by merging the two models. Here the code where I do that:
- (NSManagedObjectModel *)managedObjectModel {
if (__managedObjectModel != nil) {
return __managedObjectModel;
}
NSURL* entityURL = [[NSBundle mainBundle] URLForResource:#"User_data" withExtension:#"momd"];
NSManagedObjectModel* entityModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:entityURL];
NSURL* whoURL = [[NSBundle mainBundle] URLForResource:#"WHO_data" withExtension:#"momd"];
NSManagedObjectModel* whoModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:whoURL];
NSArray* models = [NSArray arrayWithObjects:entityModel, whoModel, nil];
__managedObjectModel = [NSManagedObjectModel modelByMergingModels:models];
return __managedObjectModel;
}
None of the attributes in my entities are optional and my app crashes when I try to save my managedObjectContext. I believe this is because some of the attributes are not being set. I have overridden awakeFromInsert: for the parent entity:
- (void) awakeFromInsert
{
[super awakeFromInsert];
NSString* userCFUUID = [[NSUserDefaults standardUserDefaults]objectForKey:#"device_identifier"];
if ( userCFUUID ) {
[self cfuuid:userCFUUID];
} else {
[NSException raise:NSInvalidArgumentException format:#"Entry: awakeFromInsert: cannot find CFUUID"];
}
[self setCreationDate:[NSDate date]]; // the time since Jan 1st 1970 in seconds
[self setEventDate:[NSDate date]];
}
But awakeFromInsert: is never called. I've set a breakpoint and stepped through from the statement where I create the NSManagedObject:
LengthEntry *length1 = [NSEntityDescription insertNewObjectForEntityForName:#"LengthEntry" inManagedObjectContext:moc];
Additional fact that may or may not be relavent:
After creating the datamodel containing the problem entity, I used the Xcode feature to automatically create the classes. I then realised that since I had not specified to do otherwise in the model, xcode named the classes in the plural sense (because that's what I had called them in the model). So, I ended up with "Entries.h" instead of "Entry.h". I went back and manually changed all the classes and specified in the model the name of the classes.
So, I need to figure out why awakeFromInsert is never called.

awakeFromInsert is called exactly once for each object when it is first created.
What you want is awakeFromFetch in order to have it called every time that it is loaded into memory from the store.
Many times, you want the same or similar code in both places.

Out of desperation, I deleted the datamodel and the NSManagedObject classes. I then recreated the model and the classes.
Now, it works. Something screwy must have happened when I manually changed the names of the classes.

Related

Core Data Migration: methods in NSEntityMigrationPolicy not called.

I have two model versions - 12 and 13. Then I created a xcmappingmodel-File with source 12 and destination 13.
I subclassed NSEntityMigrationPolicy and added my class to the mappingmodel-File to the entity desired.
#interface EndDateMigrationPolicy : NSEntityMigrationPolicy
After installing and older version (11) on my device, I install the current status with model version 13 - the app runs, but my migration methods aren't called. Am I missing something?
EDIT: is it correct to use these options?
NSDictionary *options = #{NSMigratePersistentStoresAutomaticallyOption: #YES,
NSInferMappingModelAutomaticallyOption: #YES};
I'm going to try to answer you as best I can, I have suffered through core data migrations quite a few times so I know how painful it is. For one you can not hope to have to have your migration work with those options because what you are trying to do is actually not a lightweight migration and yet you are telling it to do a lightweight migration.
Basically let's say you need for some reason to have a non lightweight migration between 2 versions, in your case 11 and 12. What you will need to do is do:
1->12 lightweight
12->13 custom migration
13->(future version) lightweight migration
There might be a better solution but I have not yet found it.
Here is some code to help you out (the hardest parts, I can't remember everything offhand), before this code I copy the database to a backup, so basically the back up database is your old one and the store is your new one (which is empty)
NSString *path = [[NSBundle mainBundle] pathForResource:<YOUR MODEL NAME> ofType:#"cdm"];
NSURL *backUpURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"<YOUR MODEL NAME>MigrationBackUp.sqlite"]; //or whatever you want to call your backup
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"<YOUR MODEL NAME>.sqlite"];
NSError *err2 = nil;
NSDictionary *sourceMetadata2 =
[NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType
URL:backUpURL
error:&err2];
NSManagedObjectModel *sourceModel2 = [NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:[NSBundle mainBundle]]
forStoreMetadata:sourceMetadata2];
NSManagedObjectModel *destinationModel2 = [self managedObjectModelForVersion:#"1.4"]; //Yeah your gonna have to get your mapping model , I'll give you this code too later
NSString *oldModel = [[sourceModel2 versionIdentifiers] anyObject];
NSLog(#"Source Model : %#",oldModel);
NSMappingModel *mappingModel = [[NSMappingModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]];
if (mappingModel != nil) {
for (NSString * identifier in [mappingModel entityMappings]) {
NSLog(#"Mapping > %#",identifier);
}
}
Then just make a migrator with your source and destination.
This is also a difficult part afterwards:
BOOL success = [migrator migrateStoreFromURL:backUpURL
type:NSSQLiteStoreType
options:nil
withMappingModel:mappingModel
toDestinationURL:storeURL
destinationType:NSSQLiteStoreType
destinationOptions:nil
error:&err2];
Last but not least (I said I'd give it to you before):
- (NSManagedObjectModel *)managedObjectModelForVersion:(NSString*)version {
NSString *modelPath = [[NSBundle mainBundle] pathForResource:#"Model" ofType:#"momd"];
if (BETWEEN_INEX(version, #"1.0", #"1.4")) {
modelPath = [modelPath stringByAppendingPathComponent:#"Model"];
modelPath = [modelPath stringByAppendingPathExtension:#"mom"];
} else if (BETWEEN_INEX(version, #"1.4", #"1.5")) {
modelPath = [modelPath stringByAppendingPathComponent:#"Model 2"];
modelPath = [modelPath stringByAppendingPathExtension:#"mom"];
} else if (BETWEEN_INEX(version, #"1.5", #"1.6")) {
modelPath = [modelPath stringByAppendingPathComponent:#"Model 3"];
modelPath = [modelPath stringByAppendingPathExtension:#"mom"];
} else if (BETWEEN_INEX(version, #"1.6", #"1.7")) {
modelPath = [modelPath stringByAppendingPathComponent:#"Model 4"];
modelPath = [modelPath stringByAppendingPathExtension:#"mom"];
}
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
NSManagedObjectModel * oldManagedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSSet *vIdentifiers = [oldManagedObjectModel versionIdentifiers];
for (NSString * identifier in vIdentifiers) {
NSLog(#"Old Model : %#",identifier);
}
return [oldManagedObjectModel autorelease];
}
I know these are just snippets of code, but I really hope it helps you solve your problem, if you need anything else just ask.
Your NSEntityMigrationPolicy is not being called because you set NSInferMappingModelAutomaticallyOption to #(YES) - it should be #(NO)

Objective-C static field issue

I've created a small class that loads dictionary items from a plist file. The getSettingForKey method works the first time I call the static method, however after a few more calls the dictionary throws a SIGABRT exception for a call with the same key that worked on a previous call. Any ideas?
static NSDictionary *dictionary = nil;
static NSLock *dictionaryLock;
#implementation ApplicationSettingsHelper
+ (void) initialize
{
dictionaryLock = [[NSLock alloc] init];
// Read plist from application bundle.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"Xxxx.plist"];
dictionary = [NSDictionary dictionaryWithContentsOfFile:finalPath];
// dump the contents of the dictionary to the console.
for(id key in dictionary)
{
NSLog(#"bundle: key=%#, value=%#", key, [dictionary objectForKey:key]);
}
}
+ (NSDictionary *)dictionaryItems
{
[dictionaryLock lock];
if (dictionary == nil)
{
[self initialize];
}
[dictionaryLock unlock];
return dictionary;
}
+(id)getSettingForKey:(NSString *)key
{
return [[self dictionaryItems] objectForKey:key];
}
#end
Moshe - I've taken your suggestion and updated to use NSUserDefaults instead:
+ (void)load
{
// Load the default values for the user defaults
NSString* pathToUserDefaultsValues = [[NSBundle mainBundle]
pathForResource:#"Xxxx"
ofType:#"plist"];
NSDictionary* userDefaultsValues = [NSDictionary dictionaryWithContentsOfFile:pathToUserDefaultsValues];
// Set them in the standard user defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsValues];
}
+ (id)getSettingForKey:(NSString *)key
{
return [[NSUserDefaults standardUserDefaults] valueForKey:key];
}
Your dictionary has probably been deallocated, causing an invalid memory access. When you create a dictionary using the dictionaryWithContentsOfFile: method, it is autoreleased, which means it will automatically be released in the future. Since you never retain the dictionary, that release will cause the dictionary to be deallocated.
Also, most of your dictionaryItems method is useless.
[dictionaryLock lock];
if (dictionary == nil) {
[self initialize];
}
[dictionaryLock unlock];
The +initialize method is automatically called by the runtime before any other method is called on your class, unless you have a +load method. Since the runtime will call it for you and it will attempt to create the dictionary, the dictionary can only be nil in the dictionaryItems method if there wasn't enough memory to create it, in which case it will fail again. Also, if you don't use the lock anywhere else, it is unnecessary also, since removing that check would cause it to be locked and immediately unlocked. Therefore, you can remove the lock and change your dictionaryItems method to simply:
+ (NSDictionary *)dictionaryItems {
return dictionary;
}
In addition to #ughoavgfhw's answer, you are also initializing dictionaryLock after you are locking it. Unless you are initializing dictionaryLock somewhere else, I'm surprised your code is getting as far as it is.
Edit: I see from #ughoavgfhw's edit that +initialize is called before anything else, so your lock is initialized there.

Why can't I get the context from the delegate with this code?

I get an annoyingly vague error from the following code:
GFree2AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
context = [delegate managedObjectContext];
context is defined as a NSManagedObjectContext in the .h file and is the same in the delegate. All the right files seem to be included (except for <CoreData/CoreData.h> in the .m file - but the program throws the same issue whether its included or not. Its included in the header file.)
All the correct frameworks and stuff are included - when I started the project I selected "use coredata to manage data" or whatever it was. So surely there shouldn't be a problem?
Is there a better way to do what I'm trying to do? Basically I don't want to have to keep passing the context through different classes till I eventually want to use it (storing data is such a small part of my application).
In the console the error I get is:
2010-08-28 13:09:24.726 GFree2[3912:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
...
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
If I comment out the line: context = [delegate managedObjectContext]; then it all seems to work fine. (at the moment I haven't acctually used any coredata or anything so theres no more code related to it.
Thanks for anyone who can help, or provide some insight into this - its so complicated.
Edit: my app delegate file methods:
#pragma mark -
#pragma mark Core Data stack
/**
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];
}
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;
}
NSString *modelPath = [[NSBundle mainBundle] pathForResource:#"GFree" ofType:#"momd"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
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 = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"GFree.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
/*
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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
#pragma mark -
#pragma mark Application's Documents directory
/**
Returns the path to the application's Documents directory.
*/
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
EDIT AGAIN:
NSString *modelPath = [[NSBundle mainBundle] pathForResource:#"GFree" ofType:#"momd"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
is the line where the error is. I'm not sure what type of file this is, but I'm sure it's obviously not finding it or something... :/ any ideas what I'm supposed to do?
#kiamlaluno - sorry for rolling back your edits, didn't mean to, just curious to see what you'd changed and thought that would show me... but it just completely removed them apparently. lol.
Edit build results:
DataModelCompile build/Debug-iphonesimulator/GFree2.app/GFree2.mom GFree2.xcdatamodel
cd "/Volumes/files/Gluten Free Cooking/Tom iPhone App/GFree2"
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/usr/bin/momc -XD_MOMC_TARGET_VERSION=10.6 "/Volumes/files/Gluten Free Cooking/Tom iPhone App/GFree2/GFree2.xcdatamodel" "/Volumes/files/Gluten Free Cooking/Tom iPhone App/GFree2/build/Debug-iphonesimulator/GFree2.app/GFree2.mom"
I believe that the problem is in persistentStoreCoordinator method in your GFree2AppDelegate.m file.
Could you update your question with the exact code of managedObjectModel, managedObjectContext and persistentStoreCoordinator methods from this file?
These methods are generated by the XCode when you check "Use Core Data for storage".
By the way, you shouldn't import .m files.
You're getting the error because GFree.momd doesn't exist. From the docs for NSURL's pathForResource:ofType: method:
Return Value The full pathname for the resource file or nil if the file could not be located.
GFree.momd is generated at build time from your .xcdatamodeld file (look in the Build Results for the line starting DataModelVersionCompile and expand it). Have you deleted or renamed that file?
Is it still listed in the Compile Sources section of your build target? (In Xcode expand Targets / [AppName] / Compile Sources).
Just noticed that your debug log says the app is called GFree2 but your code's looking for GFree.momd. Did you rename the project? If the data model file is now called GFree2.xcdatamodeld then you need to change the line that imports the momd file to use the new GFree2 name, too.

Why do I get: Illegal attempt to establish a relationship ... between objects in different contexts

I only have one managed object context.
I have modified the managed object store though.
There are certain fields that I would like to add to every managed object. It would take far too much time to add them all one by one to every object in my system, so I decided to add them programmatically to the managed object model. In the application delegate, I first do:
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
Then I iterate through all the entities in the model and modify them and set the new property array for each one:
for (NSEntityDescription *entity in entities) {
NSAttributeDescription *idAttribute = [[NSAttributeDescription alloc] init];
[idAttribute setName:#"id"];
[idAttribute setAttributeType:NSStringAttributeType];
[idAttribute setOptional:NO];
[idAttribute setIndexed:YES];
and so on.
It seems to mostly work fine. I run into problems when I am trying to add an object to a new one-to-one relationship I created. I create the objects from the same managed object context:
self.action = [NSEntityDescription insertNewObjectForEntityForName:#"MobileObjectAction" inManagedObjectContext:managedObjectContext];
self.user = [NSEntityDescription insertNewObjectForEntityForName:#"MobileUser" inManagedObjectContext:managedObjectContext];
When I get to
[user setAction:action];
I get:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'action' between objects in different contexts ...
What am I doing wrong?
Put a breakpoint in your code just before the two Entities are related. Make sure they are both instances of NSManagedObject.
If they are, make sure both of them have their NSManagedObjectContext set and it is set to the same pointer.
Obviously, make sure they are both valid objects.
Most likely one of the above tests will prove false.
I have the same problem and solve it:
one context (no splitting/merging).
set property -> and get "Illegal attempt to establish a relationship between objects..."
so - W ha T 's wrong and what I mast F ound and correct?
answer is simply:
before
[meeting setCreator: [self currentUser]];
after
[meeting setCreator: _currentUser];
and for info
*.h
#interface{
Person* _currentUser;
}
#property (readonly, retain) NSManagedObjectContext *managedObjectContext;
- (Person*)currentUser;
*.m
#synthesize managedObjectContext = _managedObjectContext;
- (Person*)currentUser{
return _currentUser;
}
- (NSManagedObjectContext *) managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return _managedObjectContext;
}

Possible to use two separate SQLite databases?

I have one sqlite database in which I store both user-defined information and information which is read-only to the user. I feel like I may need to modify the read-only information in the future, and I don't want to have to do a whole data migration. Is there a way that I can use a separate sqlite database, which can easily be replaced, for the read-only information? If so, can you give a little direction as to how this can be done? I am confused since I currently have all entities on the xcdatamodel - would I create two data models? Not sure how that would work. Thanks in advance.
This doesn't work but please feel free to give feedback.
- (NSManagedObjectModel *)managedObjectModel {
NSLog(#"%s", __FUNCTION__);
if (managedObjectModel != nil) {
return managedObjectModel;
}
//managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
NSString *mainPath = [[NSBundle mainBundle] pathForResource:#"MyApp" ofType:#"mom"];
NSURL *mainMomURL = [NSURL fileURLWithPath:mainPath];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:mainMomURL];
[managedObjectModel setEntities:[NSArray arrayWithObjects:
[[managedObjectModel entitiesByName] objectForKey:#"Version"],
[[managedObjectModel entitiesByName] objectForKey:#"Book"],
nil] forConfiguration:#"info"];
[managedObjectModel setEntities:[NSArray arrayWithObjects:
[[managedObjectModel entitiesByName] objectForKey:#"Settings"],
[[managedObjectModel entitiesByName] objectForKey:#"Persons"],
nil] forConfiguration:#"main"];
return managedObjectModel;
}
and
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
NSLog(#"%s", __FUNCTION__);
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"Main.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:#"Default" ofType:#"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSString *infoStorePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"Info.sqlite"];
if (![fileManager fileExistsAtPath:infoStorePath]) {
NSString *defaultInfoStorePath = [[NSBundle mainBundle] pathForResource:#"DefaultInfo" ofType:#"sqlite"];
if (defaultInfoStorePath) {
[fileManager copyItemAtPath:defaultInfoStorePath toPath:infoStorePath error:NULL];
}
}
NSURL *infoStoreUrl = [NSURL fileURLWithPath:infoStorePath];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
//persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] init];
NSPersistentStore *mainStore = [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:#"main" URL:storeUrl options:options error:&error];
NSPersistentStore *infoStore = [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:#"verses" URL:infoStoreUrl options:options error:&error];
NSManagedObject *settingsEntity = [[NSManagedObject alloc] initWithEntity:[[managedObjectModel entitiesByName] objectForKey:#"Settings"] insertIntoManagedObjectContext:self.managedObjectContext];
[self.managedObjectContext assignObject:settingsEntity toPersistentStore:mainStore];
NSManagedObject *persons = [[NSManagedObject alloc] initWithEntity:[[managedObjectModel entitiesByName] objectForKey:#"Persons"] insertIntoManagedObjectContext:self.managedObjectContext];
[self.managedObjectContext persons toPersistentStore:mainStore];
NSManagedObject *version = [[NSManagedObject alloc] initWithEntity:[[managedObjectModel entitiesByName] objectForKey:#"Version"] insertIntoManagedObjectContext:self.managedObjectContext];
[self.managedObjectContext assignObject:version toPersistentStore:infoStore];
NSManagedObject *book = [[NSManagedObject alloc] initWithEntity:[[managedObjectModel entitiesByName] objectForKey:#"Book"] insertIntoManagedObjectContext:self.managedObjectContext];
[self.managedObjectContext assignObject:book toPersistentStore:infoStore];
and
- (NSManagedObjectContext *)managedObjectContext {
NSLog(#"%s", __FUNCTION__);
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [NSManagedObjectContext new];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;
}
Partial answer from docs:
http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/CoreData/Articles/cdMOM.html
Configurations
A configuration has a name and an
associated set of entities. The sets
may overlap—that is, a given entity
may appear in more than one
configuration. You establish
configurations programmatically using
setEntities:forConfiguration: or using
the Xcode data modeling tool (see
Xcode Tools for Core Data), and
retrieve the entities for a given
configuration name using
entitiesForConfiguration:.
You typically use configurations if
you want to store different entities
in different stores. A persistent
store coordinator can only have one
managed object model, so by default
each store associated with a given
coordinator must contain the same
entities. To work around this
restriction, you can create a model
that contains the union of all the
entities you want to use. You then
create configurations in the model for
each of the subsets of entities that
you want to use. You can then use this
model when you create a coordinator.
When you add stores, you specify the
different store attributes by
configuration. When you are creating
your configurations, though, remember
that you cannot create cross-store
relationships.
Then NSPersistentStoreCoordinator allows you to create multiple stores each with a different configuration.
Anyone have an example of how to do all of this?
You can actually use a single data model to accomplish this, however you'll need to manually (in code) assign entities to different NSPersistentStore instances, a little bit of code:
NSPersistentStoreCoordinator *coord = [[NSPersistentStoreCoordinator alloc] init];
NSPersistentStore *userStore = [coord addPersistentStoreWithType:NSSQLiteStore configuration:nil URL:someFileURL options:someoptions error:&error];
NSPersistentStore *otherStore = [coord addPersistentStoreWithType:NSSQLiteStore configuration:nil URL:someFileURL2 options:someoptions error:&error];
//Now you use the two separate stores through a managed object context that references the coordinator
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:coord];
NSManagedObject *userObject = [[NSManagedObject alloc] initWithEntity:entityDescFromModel insertIntoManagedObjectContext:context];
[context assignObject:userObject toPersistentStore:userStore];
NSManagedObject *otherObject = [[NSManagedObject alloc] initWithEntity:entityDescFromModel insertIntoManagedObjectContext:context];
[context assignObject:otherObject toPersistentStore:otherStore];
In this way you can always specify which store the objects are kept in. I don't think you'll have to do any extra work once the objects are in their respective stores, i.e. you should just be able to execute a fetch spec in the context that references the coordinator for both stores.
Well, here's what I ended up doing. Two managedObjectModels, two managedObjectContexts, two persistentStoreCoordinators, and hence, two persistent stores. All totally separate, which is fine, since there is no relationship between the data in the two stores at all. And here is the kicker as to why sqlite files get created with no entities and no data at all: before the entities even get created you need to execute at least one fetch request in the db. Who knew? Well, obviously, not me. Anyway, this works well for me, as I won't even have the second store ready until after the app is launched (it is for an additional feature). Now, when my data file is finally ready, I can just add the sqlite file, uncomment the code pertaining to it, and send the app to the app store. It won't touch the store with the user data in it. And, I am going to keep my read-only store in my bundle so no migration. How's that sound?
Ok, I found out how to add another data model. File>New File>Iphone OS>Resource>Data Model. Moved my Entities to that Data Model. Compiled and seems to run, but with no data. Problem is, still have just one sqlite file. Need to find out how to use two, and associate each with appropriate model. Then, should be able to overwrite default sqlite file for new model on app update. BUT I will still have to do a migration, I think, since it will have created a sqlite file on the iPhone from the default file I specify. It shouldn't be a hard migration I hope since I won't have any user data in the file. Learning, but still, any further assistance appreciated.