0

我有一个基于 Tim Roadlys 教程的 coredata 应用程序。该应用程序使用标签栏,其中一个加载核心数据。我有另一个选项卡加载一个UIView,然后我通过一个按钮链接到另一个tableView也将具有核心数据的选项卡。

问题是单击按钮时第二个视图崩溃。我是通过 segue 传递托管对象上下文还是必须将其放入appdelegate

谢谢你的帮助 :)

这是转场

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{


    if ([segue.identifier isEqualToString:@"Load Ancients Segue"]) {
        UITabBarController *tabBarViewController = (UITabBarController *) [segue destinationViewController];
        AncientsInventoryTVC *ancientsInventoryTVC = (AncientsInventoryTVC *) [[[tabBarViewController viewControllers] objectAtIndex:5] topViewController];
        ancientsInventoryTVC.managedObjectContext = self.managedObjectContext;

}

} 

当我单击按钮加载视图时,加载以下内容时出现错误;这就是我认为我正在用segue解决的问题。self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

错误是 - 一个实例NSFetchedResultsController需要一个非零fetchRequestmanagedObjectContext

4

3 回答 3

0

这不是很多信息和零代码,但我会把它放在 prepareForSegue 调用中

于 2012-12-08T03:41:28.213 回答
0
@interface DataServer : NSObject

   +(DataServer *) get;

   @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
   @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
   @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator      *persistentStoreCoordinator;

@end



@implementation DataServer

@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;

DataServer * _appData;

+(DataServer *) get {
    if (_appData == nil) {
    _appData = [[DataServer alloc] init];
}
return _appData;
}

- (NSManagedObjectContext *)managedObjectContext
{
    if (__managedObjectContext != nil) {
        return __managedObjectContext;
    } 

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        __managedObjectContext = [[NSManagedObjectContext alloc] init];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    [__managedObjectContext setMergePolicy: NSOverwriteMergePolicy];
    }
    return __managedObjectContext;
}

- (NSManagedObjectModel *)managedObjectModel
{
    if (__managedObjectModel != nil) {
        return __managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel" withExtension:@"momd"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return __managedObjectModel;
} 

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{

    if (__persistentStoreCoordinator != nil) {
        return __persistentStoreCoordinator;
    }

    NSString * dbName = @"MyDbName.sqlite";
    NSURL *storeURL = [[self applicationDocumentsDirectory] 
    URLByAppendingPathComponent: dbName];

    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] 
    initWithManagedObjectModel: self.managedObjectModel];

     // request automatic migration
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
    [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
    if (![__persistentStoreCoordinator 
        addPersistentStoreWithType:NSSQLiteStoreType 
        configuration:nil URL:storeURL options: options error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        [[[UIAlertView alloc ]
            initWithTitle: @"Internal Error" 
            message: [NSString stringWithFormat: @"%@", [error userInfo]]
            delegate:nil 
            cancelButtonTitle: @"OK" 
            otherButtonTitles: nil] show];
      }    

       return __persistentStoreCoordinator;
    }


- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] 
    URLsForDirectory:NSDocumentDirectory 
    inDomains:NSUserDomainMask] 
    lastObject];
}
于 2012-12-09T02:55:33.320 回答
0

为什么不创建一个单独的单例类,在其中保留 MOC 并从两个控制器中使用它,而不是在控制器中创建 MOC?

于 2012-12-08T16:30:38.733 回答