how to get mapc to compile a mapping model - core-data-migration

We're successfully been using CoreData with Xamarin Studio, but I ran into a problem when trying to create a mapping model because we altered all the entities to subclass off of a single entity. I created the mapping model in Xcode as normal, but when I go to compile it using mapc like this:
xcrun mapc Model.xcmappingmodel .
this is the result:
mapc error - mapc failed to archive result to .
Of course I check and I do have write access to the current directory.

First, do not extend all of your entities off a single entity. That will create a very bad data model. Guessing that you are using SQLite, that will produce a single table model that will perform poorly.
What happens when you try and compile your model directly in Xcode?

Related

Command Line Tool Core Data Tutorial

I'm playing with Xcode and Objective-C/Cocoa again. This time I want to start with the bare minimum. I would like to setup Core Data in something as simple as a Command Line Tool (if it's even possible.) I need some practice saving and retrieving data without the all the View Controllers and AppDelegate stuff. Any ideas or maybe I'm going about this all wrong?
Your task will be the same as for any other project, except that the Command Line Tool template probably doesn't contain a Core Data option. No matter, you're doing this to learn, so setting up Core Data yourself won't hurt a bit.
Just create a new command line tool project and add a new Core Data model to the project. Next, you'll add the code that gets the model, creates a persistent store coordinator, uses those to create a managed object context. Then you can add objects, fetch objects, whatever you like.

Coredata lightweight migration "Can't find mapping model for migration"

I am doing a coredata database migration using lightweight migration in xcode4.5, I kept on getting "Can't find mapping model for migration" when I set "NSInferMappingModelAutomaticallyOption" to "NO". If I set "NSInferMappingModelAutomaticallyOption" to "YES", lightweight migration passes without problem.
Here is the steps I followed:
add a new model version .xcdatamodelId
make changes on the entities (including adding a new entity)
select the newer versioned datamodel as the current version, generate the new NSManagedObject subclasses, and make correspondant changes to the code.
create a mapping model and make the source points to the old datamodel version and the destination points to the new datamodel version
create custom migration policy and hook it up with one of the mappings inside the
mapping model
set up lightweight migration with "NSInferMappingModelAutomaticallyOption" equals to "NO".
test migration on simulator with the database coming from an older build.
I followed all and steps talked on apple documents and didn't make any changes on the entity schema after creating the mapping.. I tried to clean the DerivedData folder in xcode, and also I check the "VersionInfo.plist" which contains the correct versions of my datamodel. For the unchanged entities their hashkey are matching.
However I still get this error when I tried to do an migration.... Quite frustrated now.. Anyone can help to give me some guid on this problem?

modyfing a sqlite db mapped via core data throws errors: uncaught exception of class 'NSCFString'

I have an iphone app that has a sqlite db, mapped to core data. All the data manipulation in the app is via Coredata.
On one table I wanted to add a string attribute. And I did two things both with similar crashes:
I tried to use Mesasql to alter the structure. I easily added a Varchar column. But it crashes.
On a separate attempt, with the mapped SQLlite db, restored, I tried adding a attribute to the entity in core data and it crashed.
This is the error I got:
* Terminating app due to uncaught exception of class 'NSCFString'
terminate called after throwing an instance of 'NSCFString'
So, how can I alter the structure of the sqlite table mapped to core data without breaking the app? Am I fogetting to do a commit or something like that?
Please help
Every time you modify an entity you have to delete the sqlite store. If your application has already shipped, the alternative is to migrate your data. This SO answer offers a simple explanation of how to do that.

NSArrayController and referring to a shared, static, Core Data based library

Using this guide I have created a static library (let's call it AppCore) that can be shared between the Mac OS X and iOS versions of one app. This static library uses Core Data and the point of it is to share the model part and schema versioning between different implementations.
I created a NSPersistentDocument based project that will depend on this AppCore. In this project I added a reference to the .xcdatamodel file. Then I created a simple table view with add/remove buttons to edit an array of one entity type with the assisted "new core data entity" item. This created an instance of NSArrayController and the required bindings for the add/remove behaviour.
Now, everything seems to work fine when I'm using the default class for the Core Data entities (NSManagedObject) and I'm able to add new rows using the +/- buttons. However, when I change the entity implementation class to a custom one, I'm getting an error
Failed to create new object
This seems to come from the NSArrayController and it seems to be unable to instantiate the required entity. I can, however, create one in the NSPersistentDocument subclass by:
[NSEntityDescription insertNewObjectForEntityForName:#"SomeEntity" inManagedObjectContext:[self managedObjectContext]]
What confuses me is why the instance of NSArrayController can't. If I understand correctly, the array controller is instructed to create an entity, not class and my guess is that the entities are created with the help of NSEntityDescription class. I could implement my own version of the array controller's add: but then again, it might be that here something is fundamentally wrong. I haven't touched the init:s and the custom entity class implementation is simply for convenience, to access the attributes directly.
I have tried changing the base SDK on the AppCore but without effect. Currently it uses the iOS version but I'm not sure how it should be. This is another question but if unrelated, I might ask it here on a separate question.
So, to summarize, why can't the NSArrayController create an instance of this entity?
Thanks in advance.
Update
This works if I add the SomeEntity class from the AppCore to the dependent project as a reference. This is not the most usable way since modifications to the AppCore has to be propagated to the dependatnt projects also.
Bingo. I missed the "-ObjC" flag for the dependant project's "other linker flags". Now everything works like a charm.

How to preserve compatibility when loading NSPersistentDocument(s) saved using a modified entity model?

I've created an OSX app that uses the entity model builder and its related stuff, for simplicity consider the entity with only two NSString.
The app is in production and works fine, now I need to add new attributes to the existing entity (only one entity exists), but with new attributes the old saved files are not open, silently the app does't open them and the console doesn't contain any error/warning message.
I need to load old saved files, consider all new attributes are optionals and have defaults (also in code not only in model design)
All existing attributes continue to be present I've only added the new attrs.
How can I design applications able to work when the entity model change?
From OS X 10.5 onwards, there is data migration functionality to help you, as long as you make changes to the data model in a new version.
There is a good basic explanation and example here: http://www.timisted.net/blog/archive/core-data-migration/
and the Apple documentation is here: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreDataVersioning/Introduction/Introduction.html

Resources