核心数据和UITabBar控制器 - 帮助? [英] Core Data and UITabBar Controller - help?

查看:107
本文介绍了核心数据和UITabBar控制器 - 帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个项目与UITabBarController和一些导航控制器,我试图实现Core Data。它只是不工作。



我有一些奇怪的设置:
UITabBarController - >导航控制器 - >表视图控制器



我已经复制了所有的核心数据代码,并添加了一个具有属性('Event'和'name' - 与教程一样)的实体。我不断收到错误:
由于未捕获异常NSInternalInconsistencyException导致应用程序失败,原因:'+ entityForName:找不到实体名称Event的NSManagedObjectModel



错误只发生在我切换到表视图,我想填充Core Data内容。



我发现错误发生在表中的这一行View Controller:



NSEntityDescription * entity = [NSEntityDescription entityForName:@EventinManagedObjectContext:managedObjectContext]; p>

这似乎与此对应(在应用程序代表中):

  NSManagedObjectContext * context = [self managedObjectContext]; 

if(!context){

//处理错误。

NSLog(@\\\
不能为自己创建*上下文);

 } 

rootViewController.managedObjectContext = context;



任何帮助?



更新:我设法使其工作(非常令人兴奋的时刻,斯坦福赢得了一半 - 好日子)。我现在引用它从应用程序代表。 )

解决方案

您的管理对象上下文可能未设置,实体事件显然不是对于nil上下文有效。



我在所有视图控制器中使用对我的应用程序委托的引用,以便他们可以访问一个托管对象上下文。听起来像其他人经常使用单例管理Core Data,并从中获取管理对象上下文。



UPDATE



在Cocoa / Cocoa Touch应用程序中放置Core Data Stack的位置。



下面是一些用于在应用委托中保留Core Data堆栈的示例代码:



使用Apple的标准Core在YourAppDelegate中实现数据堆栈。 managedObjectContext实现为一个示例,但managedObjectModel和persistentStoreCoordinator也必须实现。



YourAppDelegate.h

  @interface YourAppDelegate:NSObject< UIApplicationDelegate> {
// Core Data stuff
NSManagedObjectModel * managedObjectModel;
NSManagedObjectContext * managedObjectContext;
NSPersistentStoreCoordinator * persistentStoreCoordinator;

//其他应用程式ivars
}

YourAppDelegate.m

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

在每个视图控制器中,获取对应用代理的引用,根据需要获取managedObjectContext。例如,当设置fetchedResultsController;



RootViewController.h

  @interface RootViewController:UITableViewController< NSFetchedResultsControllerDelegate> {
NSFetchedResultsController * fetchedResultsController;

YourAppDelegate * app;
}

RootViewController.m

  #importRootViewController.h
#importYourAppDelegate.h

@implementation RootViewController

@synthesize fetchedResultsController;

- (void)viewDidLoad {
[super viewDidLoad];

app =(YourAppDelegate *)[UIApplication sharedApplication] .delegate;
}

- (NSFetchedResultsController *)fetchedResultsController {
if(fetchedResultsController!= nil){
return fetchedResultsController;
}

NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription * entity = [NSEntityDescription entityForName:@EventinManagedObjectContext:app.managedObjectContext];
[fetchRequest setEntity:entity];

//设置批处理大小,谓词和&排序键等

NSFetchedResultsController * aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:app.managedObjectContext sectionNameKeyPath:nil cacheName:@Root];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

[aFetchedResultsController release];
[fetchRequest release];

return fetchedResultsController;
}


So I have a Project with a UITabBarController and a few Navigation Controllers, and I am trying to implement Core Data. Its just not working.

I have a bit of a weird setup: UITabBarController -> Navigation Controller -> Table View Controller

I have copied all of the Core Data code and added an entity with an attribute ('Event' and 'name' - just like the tutorials). I keep getting the error: erminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Event''

The error only occurs when I switch to the Table View I want populated by the Core Data content.

I found that the error occurs on this line in the Table View Controller:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];

This seems to correspond with this (in the App Delegate):

NSManagedObjectContext *context = [self managedObjectContext];

if (!context) {

    // Handle the error.

NSLog(@"\nCould not create *context for self");

}

rootViewController.managedObjectContext = context;

Any Help?

Update: I managed to get it working (very exciting moment, and Stanford is winning at the half - so far its a good day). I am now referencing it from the App Delegate. Ahhh, this feels soo good :)

解决方案

Your managed object context is probably not set and the entity "Event" is obviously not valid for a nil context.

I use a reference to my app delegate in all my view controllers so they can access the one managed object context. It sounds like others often use a singleton to manage Core Data and would get the managed object context from that.

UPDATE

There is a good discussion about where to keep the Core Data stack in Where to place the "Core Data Stack" in a Cocoa/Cocoa Touch application.

Here is some example code for keeping the Core Data stack in the app delegate:

Use Apple's standard Core Data stack implementation in YourAppDelegate. managedObjectContext is implemented as an example, but managedObjectModel and persistentStoreCoordinator must be implemented as well.

YourAppDelegate.h:

@interface YourAppDelegate : NSObject <UIApplicationDelegate> {
    // Core Data stuff
    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;       
    NSPersistentStoreCoordinator *persistentStoreCoordinator;

    // other app ivars
}

YourAppDelegate.m:

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

In every view controller, get a reference to the app delegate and use it to get the managedObjectContext as needed. For example, when setting up the fetchedResultsController;

RootViewController.h:

@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
    NSFetchedResultsController *fetchedResultsController;

    YourAppDelegate *app;
}

RootViewController.m:

#import "RootViewController.h"
#import "YourAppDelegate.h"

@implementation RootViewController

@synthesize fetchedResultsController;

- (void)viewDidLoad {
    [super viewDidLoad];

    app = (YourAppDelegate*)[UIApplication sharedApplication].delegate;
}

- (NSFetchedResultsController *)fetchedResultsController {
    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:app.managedObjectContext];
    [fetchRequest setEntity:entity];

    // setup the batch size, predicate, & sort keys, etc

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:app.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];

    return fetchedResultsController;
}    

这篇关于核心数据和UITabBar控制器 - 帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆