我的项目(用于管理学生参加各种课程)包含2个xkcddatamodel文件,一个用于学期开始/结束日期,另一个用于课程数据。如果您查看AppDelegate.m
,我会将modelURL
设置为第二个模型。我该如何为第一个xkcddatamodel
创建第二个上下文?我已经尝试了here给出的解决方案。但没有骰子。 Xcode甚至没有认出它,并要求我用managedObjectContext2
替换managedObjectContext
。我该如何解决这个问题?
项目的Dropbox链接:https://www.dropbox.com/sh/ivl6nw6nw8p9j1y/MDwYDaHVH_
这是AppDelegate.m
//
// AppDelegate.m
// timetable app
//
// Created by Robin Malhotra on 22/12/13.
// Copyright (c) 2013 Robin's code kitchen. All rights reserved.
//
#import "AppDelegate.h"
#import <CoreData/CoreData.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
// Explicitly write Core Data accessors
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
// removed some code about retain here as ARC hates that
return managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
stringByAppendingPathComponent: @"Courses.sqlite"]];
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"ClassesAttended" withExtension:@"momd"];
managedObjectModel=[[NSManagedObjectModel alloc]initWithContentsOfURL:modelURL];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeUrl options:nil error:&error]) {
/*Error for store creation should be handled in here*/
}
return persistentStoreCoordinator;
}
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
/* (...Existing Application Code...) */
-(NSArray *)getAllCourses
{
NSFetchRequest *request=[[NSFetchRequest alloc]init];
NSEntityDescription *entitydesc=[NSEntityDescription entityForName:@"Course" inManagedObjectContext:managedObjectContext];
[request setEntity:entitydesc];
NSError *error;
NSArray *fetchedCourses=[self.managedObjectContext executeFetchRequest:request error:&error];
return fetchedCourses;
}
@end
这是AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
}
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, readonly) NSManagedObjectModel *managedObjectModel2;
@property (nonatomic, readonly) NSManagedObjectContext *managedObjectContext2;
@property (nonatomic, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator2;
- (NSString *)applicationDocumentsDirectory;
- (NSArray *)getAllCourses;
@end