开发者

Using Core Data together with SQLite - Why in some example withExtension:@"momd" is used?

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 = [[NS开发者_如何学CBundle 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???


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.


*.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.


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜