1

I am using core data with sqlite.. I found in one ready app that in AppDelegate

-(NSManagedObjectModel *) managedObjectModel
{
    if(__managedObjectModel != nil)
    {
         return __managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MYSQLite" withExtension:@"momd"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

    return __managedObjectModel;
}

In this example though my SQLite database is having .sqlite extension... this example is having .momd extension.... What is the reason behind that???

DShah
  • 9,768
  • 11
  • 71
  • 127

3 Answers3

19

The .mom and .momd files are the compiled data model files. These would be analogous to files that define the schema for an SQL database although they have absolutely nothing to do with SQL and can be used with any type or Core Data persistent store.

The data model is what you create in the data model editor where you graphically define the entities and the relationships between entities. The data model defines how the objects relate to one another so that the managed object context can "manage" the objects at runtime. It is also used by the presistent store to "freeze dry" the objects into a file on disk for persistent storage.

When you compile a project, the .xcdatamodel file is compiled into either a .mom file or a .momd file. The former is for an unversioned model and the later is for versioned model. These days, you almost always see a .momd file. The compiled file only appears inside the app bundle. It does not appear as a project file any more than the .o files of compiled source code appear in the project files.

By contrast, the .sqlite or .plist or other file extensions are the actual persistent store file that contain the app's data. Which type of file you get depends on the type of store that use. The type of compiled model file you have is completely irrelevant to the type of persistent store you use. You can use the same model with virtually all types of stores. The only limitation is that once a store is created with a model you must use that same model to access the store from that point onward. If you change the model, you must migrate the store to the new model.

So, this code:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MYSQLite" withExtension:@"momd"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

... is completely wrong because it confuses the two types of files. At this point, you are just loading the data model so that the context and persistent store will understand how the data fits together. Normally, the code would look like:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"appNameDataModel" withExtension:@"momd"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

(Note as well, that the data model file is stored inside the app bundle and as such is always readonly.)

Once you have a managed object model, you then provide it to a persistent store coordinator like so:

persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

... and then you create or open the actual persistent store file which, if you use an sqlite store, is the .sqlite file:

[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]

The persistent store coordinator will use the information in the .momd file to understand how the data will be configured when it is stored in and retrieved from the persistent store file.

In addition, make sure you understand that Core Data is not some kind of object SQL wrapper. Core Data has nothing to do with SQL. The SQLite store is just one of several store types that Core Data can use behind the scenes. The actual operation of Core Data does not require SQL at all and many Core Data apps never use SQL at all.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • Just for getting clear idea, Is it that though we store data using Core Data we are finally dealing with SQLite only??? right?? Finally we are fetching data from SQLite only.. So core data is giving another way to fetch data from sqlite? And What could be the criteria to decide which one to use in our app?? sqlite or core data?? By criteria i mean, Is there like more security, scalability, OR some other criteria?? – DShah Aug 02 '11 at 04:59
  • 2
    Core Data's primary function is to create the model layer of a Model-View-Controller design app. It is primarily concerned with managing a live collection of objects, an object graph, at runtime. Persisting that object graph to disk is optional and there are several different persistent formats e.g. sqlite, xml, binary, custom. It's a mistake to think of Core Data as being built around sqlite because sqlite is a recent addition to Core Data and Core Data works fine without it. – TechZen Aug 03 '11 at 20:29
  • For some insight of when to use Core Data, straight SQL or something else, see this previous answer of mine http://stackoverflow.com/questions/5237943/creating-a-json-store-for-iphone/5249674#5249674 – TechZen Aug 03 '11 at 20:30
2

*.momd is not your data file (SQLite file), it your model, where you have create the object that are stored in the coredatafile.

Momd is short for managed object model.

The persistant store is where you set the datafile, which in your case the a SQLite file.

hwaxxer
  • 3,363
  • 1
  • 22
  • 39
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Well CoreData isn't a ORM, its an object graph. Which means that it holds object with relations to other objects. The object managed bij coredata are modeled in the momd file. – rckoenes Aug 01 '11 at 12:25
0

Core Data provides an architecture to support versioning of managed object models and migration of data from one version to another.

You should read this document...

Maulik
  • 19,348
  • 14
  • 82
  • 137
  • so how are you relating your answer with my question??? can you please explain... I refered the link you have given...but it explains me about migration and organization of data.... – DShah Aug 01 '11 at 13:01
  • yeah in fact your question is about to migration and organization of object. your object will not copied into your application bundle as-is. Instead, one gets compiled into a new file of type .mom (managed object model). When you add a new version to your project, the current version gets compiled into a .mom file in your application's bundle in the Resources folder. – Maulik Aug 01 '11 at 13:17
  • 1
    It doesn't stop there, thought. It also creates a folder (technically, a bundle) in Resources named after your data model but with a .momd extension. Inside the .momd bundle, it puts a compiled .mom file for each of the other versions of your data model. This is understandable. It needs this information to do the automatic migration. from: http://iphonedevelopment.blogspot.com/2009/09/core-data-migration-problems.html – Maulik Aug 01 '11 at 13:18