Override managedObjectModel in UIManagedDocument

I am working with the new UIDocument features in iOS 5.0. I have an application that uses several different data models (MOMD files). According to the documentation for UIManagedDocument, you can override - (NSManagedObjectModel *) managedObjectModel to load a specific data model (by default, all found data models loaded together are loaded). Both data models have overlapping model names with different schemas, so in my case this is undesirable.

Corresponding Apple Doc

So, the problem that I encountered in a simple example is that I can override a function, but I can not assign a result to it. It is private, so the subclass cannot access _managedObjectModel; and, this is read-only, therefore self.managedObjectModel cannot be assigned ..

I was looking for a UIManagedDocument example that overrides managedObjectModel, but Apple doesn't seem to provide it.

I can define a new _myManagedObjectModel instance variable and assign it. Then return this to the access method that I override. I am concerned that this may violate some internal implementation of UIManagedDocument, which does not use the managedObjectModel accessor in preference for _managedObjectModel (which is common in Apples implementations ...)

It seems like a direct problem, and I suspect I'm just missing out on something really simple to allow the correct override.

//
//  DTNoteDocument.m
//  document-test
//
//

#import "DTNoteDocument.h"

@implementation DTNoteDocument

NSString * const kDataManagerModelName = @"Note";

-(NSManagedObjectModel*)managedObjectModel {

    if (_managedObjectModel != nil)
        return _managedObjectModel;

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *modelPath = [bundle pathForResource:kDataManagerModelName ofType:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]]; // compile error on this line, private variable cannot be assigned

    return _managedObjectModel;
}
@end

Title:

#import <UIKit/UIKit.h>

@interface DTNoteDocument : UIManagedDocument

@end
+5
source share
2 answers

I decided this last week, but decided that I’ll do it to reflect my decision. If you find something better, I would be glad to see it.

I added a unique property to the class title to run:

@property (nonatomic,retain,readonly) NSManagedObjectModel *myManagedObjectModel;

Then the following is added to my class implementation:

-(NSManagedObjectModel*)myManagedObjectModel {
    if (myManagedObjectModel)
        return myManagedObjectModel;

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *modelPath = [bundle pathForResource:kDataManagerModelName ofType:@"momd"];
    myManagedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];

    return myManagedObjectModel;
}

-(NSManagedObjectModel*)managedObjectModel {
    return self.myManagedObjectModel;
}

get managedObjectModel, . UIManagedDocument ​​. , managedObjectModel init, , .

+5

, . UIManagedDocument objectmodelLoading...

:

.m file

#import <CoreData/CoreData.h>

@implementation DTNoteDocument{
    NSManagedObjectModel * myManagedObjectModel;
}
0

All Articles