1

My project(for managing students' attendance in various classes) contains 2 xkcddatamodel files, one for the semester start/end dates and the other for the course data. If you look at AppDelegate.m, I've set the modelURL to the second model. How am I supposed to create a second context for the first xkcddatamodel? I've tried the solution given here. But no dice. Xcode isn't even recognising it and is asking me to replace managedObjectContext2 with managedObjectContext. How do I fix this?

Dropbox link to the project: https://www.dropbox.com/sh/ivl6nw6nw8p9j1y/MDwYDaHVH_

Here's the 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

and here's the 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
Cœur
  • 37,241
  • 25
  • 195
  • 267
Robin
  • 11
  • 4
  • Do you need two separate xcdatamodel files? – Rogiel Sulzbach Dec 28 '13 at 21:01
  • FYI: Links to external content are discouraged. Please post *relevant* code in your question. – marko Dec 28 '13 at 21:08
  • Yeah. I'll be needing 2 xcdatamodelfiles. I'll post the relevant code – Robin Dec 28 '13 at 21:35
  • Oh,and this is the other stackoverflow question I found which didn't work http://stackoverflow.com/questions/4170740/can-a-iphone-ipad-universal-app-have-two-different-xcdatamodel-files – Robin Dec 28 '13 at 22:05
  • Also, in the answer given here[link](http://stackoverflow.com/questions/16146289/is-it-possilbe-to-have-mutliple-core-data-model-files-to-one-single-xcode-projec), there is a "somecondition" at the end. What can I set as this condition – Robin Dec 28 '13 at 22:08
  • It looks like what you want can be done in a single xcdatamodel file... You don't need a file per entity, a single file can represent multiple entities. In fact, if you use multiple files you end with multiple databases, which is not nice. – Rogiel Sulzbach Dec 29 '13 at 01:34
  • Okay, so I've put them in one xcdatamodel, but for the sake of the argument, how do you do this? – Robin Dec 29 '13 at 15:33

0 Answers0