Question

I have found these Stanford tutorials https://itunes.apple.com/us/course/ipad-iphone-app-development/id495052415, and have been listening a lecture about core data, which is really great.

It shows how to access Core Data via UIManagedDocument... Only thing I don't understand how UIManagedDocument knows which model should it use, because I don't see it set anywhere?

To sum it when using UIManagedDocument, how do you define database model that is gonna be used?

Was it helpful?

Solution

I found the answer:

'UIManagedDocument' takes all the models from your application's main bundle, and makes union of these models. If you only have one model that model is used.

This can be changed by overriding 'UIManagedDocument' class.

OTHER TIPS

Loading a Data Model In some cases, you do not have to write any code to load a model. If you use a document-based application on OS X, NSPersistentDocument manages the task of finding and loading your application’s model for you. If you use Xcode to create a non-document application that uses Core Data (for OS X or for iOS), the application delegate includes code to retrieve the model. The name of a model—as represented by the filename used to store it on disk—is not relevant at runtime. Once the model is loaded by Core Data, the filename is meaningless and has no use, so you can name the model file whatever you like.

If you want to load a model yourself, there are two mechanisms you can use:

You can load a single model from a specific URL, using the instance method initWithContentsOfURL:. This is the generally-preferred technique. Typically an application has a single model, and using this method you ensure that you load only that model. You can also load individual models via URLs and then unify them using modelByMergingModels: before instantiating a coordinator with them.

In cases where you have more than one model—and particularly in cases where the models represent different versions of the same schema—knowing which model to load is essential (merging together models with the same entities at runtime into a single collection would cause naming collisions and errors). This method is also useful if you want to store the model outside of the bundle for your application, and so need to reference it via a file-system URL.

You can create a merged model from a specific collection of bundles, using the class method mergedModelFromBundles:. This method may be useful in cases where segregation of models is not important—for example, you may know your application and a framework it links to both have models you need or want to load. The class method allows you to easily load all of the models at once without having to consider what the names are, or put in specialized initialization code to ensure all of your models are found.

Accessing and Using a Managed Object Model at Runtime

    NSManagedObjectModel *model = <#Get a model#>;
NSFetchRequest *requestTemplate = [[NSFetchRequest alloc] init];
NSEntityDescription *publicationEntity =
    [[model entitiesByName] objectForKey:@"Publication"];
[requestTemplate setEntity:publicationEntity];

NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:
    @"(mainAuthor.firstName like[cd] $FIRST_NAME) AND \
        (mainAuthor.lastName like[cd] $LAST_NAME) AND \
        (publicationDate > $DATE)"];
[requestTemplate setPredicate:predicateTemplate];

[model setFetchRequestTemplate:requestTemplate
    forName:@"PublicationsForAuthorSinceDate"];

Using a fetch request template

NSManagedObjectModel *model = <#Get a model#>;
NSError *error = nil;
NSDictionary *substitutionDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
    @"Fiona", @"FIRST_NAME", @"Verde", @"LAST_NAME",
    [NSDate dateWithTimeIntervalSinceNow:-31356000], @"DATE", nil];
NSFetchRequest *fetchRequest =
    [model fetchRequestFromTemplateWithName:@"PublicationsForAuthorSinceDate"
            substitutionVariables:substitutionDictionary];
NSArray *results =
    [aManagedObjectContext executeFetchRequest:fetchRequest error:&error];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top