【发布时间】:2012-03-09 03:01:36
【问题描述】:
我已经开始创建一个在后端使用核心数据堆栈的应用程序。它在堆栈中有各种实体(特别是 3 个),每个实体都可由不同的表视图控制器访问。因此,例如,我有一个表视图控制器可以访问的“客户端”实体。一旦选择了一个客户端,就会调用一个新的表视图控制器来显示“汽车”实体等中的记录...
目前,我遵循 Apple 的建议,将“managedObjectContext”传递到在 AppDelegate 中创建的链(可以这么说)。这确实有效,但这意味着我在堆栈中访问、添加和删除 managedObjects 的所有代码都集成在我的所有视图控制器中。
我认为创建一个“CoreDataModel”类来处理与我的堆栈的所有交互并且每个视图控制器都可以根据需要调用它会更简洁(并且是 MVC 的更好实现)。
首先,这看起来合理/可行吗?
其次,在我的实现中,我将所有核心数据设置代码保留在 AppDelegate 中,并将 managedObjectContext 分配给我的 CoreDataModel 类的新实例。然而,在我的第一个视图控制器中,我调用了我的 CoreDataModel 类的一个实例方法,称为 (NSMutableArray *)retrieveClientList{}。当方法被正确调用时,我已经设置了一个小的 NSLog 报告,但这似乎没有被调用。
作为帮助,我粘贴了自定义类中的代码,以及 AppDelegate 和第一个视图控制器。 [在第一个 tableviewcontroller 之前有一个 rootviewcontroller,它只是一个主菜单,我没有在这里粘贴,因为我认为这并不重要]
任何指针,不胜感激...
这是我的自定义 CoreDataModel 类的实现...
@implementation CoreDataModel
@synthesize managedObjectContext;
-(NSMutableArray *)retrieveClientList {
// Create fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Client" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if(mutableFetchResults == nil) {
// Handle the error
}
NSLog(@"Got here!");
return mutableFetchResults;
}
@end
这是我的 AppDelegate 的实现...
@implementation iPTAppDelegate
@synthesize window;
@synthesize navigationController;
@synthesize managedObjectContext;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create new CoreDataModel object
CoreDataModel *coreDataModel = [[CoreDataModel alloc] init];
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
//Handle the error
}
//Pass the managed object context to the new CoreDataModel object
coreDataModel.managedObjectContext = context;
//Set the navigation controller as the window's root view controller and display
RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
[coreDataModel release];
[rootViewController release];
[aNavigationController release];
return YES;
}
最后,这是我的第一个视图控制器...
#import "ClientListViewController.h"
#import "ClientViewController.h"
#import "CoreDataModel.h"
@implementation ClientListViewController
@synthesize clientsArray;
@synthesize coreDataModel;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// Set the title
self.title=@"Clients";
// Add the + button
// self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editMode)] autorelease];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(callAddClientViewController)] autorelease];
[self populateTable];
}
-(void)populateTable {
NSLog(@"Called here");
[self setClientsArray:[coreDataModel retrieveClientList]];
}
【问题讨论】: