Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are my core data entities?

I have a pre-existing project to which I've added Core Data models. I added the Core Data framework, added a data model with entities, and included it in my app's target, along with some generated NSManagedObject classes. It compiles nicely, and now I'd like to add some tests for the entities I've created. Following these instructions, I've set up a logic test base class with a setUp method like so:

- (void)setUp {
    model = [NSManagedObjectModel mergedModelFromBundles:nil];
    NSLog(@"model: %@", model);
    coord = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
    store = [coord addPersistentStoreWithType:NSInMemoryStoreType
                                configuration:nil
                                          URL:nil
                                      options:nil 
                                        error:NULL];
    ctx = [[NSManagedObjectContext alloc] init];
    [ctx setPersistentStoreCoordinator:coord];
}

This compiles and all the objects are created. However, the model has no entities! The NSLog() output looks like so:

2011-10-29 23:56:58.941 otest[42682:3b03] model: (<NSManagedObjectModel: 0x19c6780>) isEditable 1, entities {
}, fetch request templates {
}

So where are my entities? I've poked around the bundle, and there are no .momd files, either. Have I missed some crucial step to get my models to build?

like image 475
theory Avatar asked Oct 30 '11 07:10

theory


People also ask

Where is Core Data stored?

The persistent store should be located in the AppData > Library > Application Support directory.

What is a Core Data entity?

Core Data is an object graph and persistence framework provided by Apple in the macOS and iOS operating systems. It was introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0. It allows data organized by the relational entity–attribute model to be serialized into XML, binary, or SQLite stores.

How can I recover data from Core Data?

Fetching Data From CoreData We have created a function fetch() whose return type is array of College(Entity). For fetching the data we just write context. fetch and pass fetchRequest that will generate an exception so we handle it by writing try catch, so we fetched our all the data from CoreData.

What is a data entity?

A data entity is an abstraction from the physical implementation of database tables. For example, in normalized tables, a lot of the data for each customer might be stored in a customer table, and then the rest might be spread across a small set of related tables.


1 Answers

I did some additional Duck Duck Going and managed to find the information I needed in the this answer. The upshot is that, because a test target doesn't use a "main" bundle, I have to instantiate the test bundle. So instead of this line:

    model = [NSManagedObjectModel mergedModelFromBundles:nil];

I now have these three lines:

    NSBundle *bundle = [NSBundle bundleWithIdentifier:@"com.example.LogicTests"];
    NSURL *url = [bundle URLForResource:@"MyModels" withExtension:@"momd"];
    model = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];

The bundle identifier comes directly from my target build info, while "MyModels" comes from my data model file, which is named "MyModels.xcdatamodeld" and included in the app bundle as "MyModels.momd". And that, of course, contains my models.

like image 172
theory Avatar answered Sep 28 '22 16:09

theory