我已经开始创建一个在后端使用核心数据堆栈的应用程序。它在堆栈中有各种实体(特别是 3 个),每个实体都可以由不同的 TableView Controller 访问。因此,例如,我有一个可由 TableView Controller 访问的“客户端”实体。一旦选择了客户端,就会调用一个新的 TableView Controller ,它会显示“汽车”实体等中的记录...
目前,我已经按照 Apple 的建议将“managedObjectContext”传递到在 AppDelegate 中创建的链(可以这么说)。这确实有效,但这意味着我在堆栈中访问、添加和删除托管对象的所有代码都集成在我所有的 View Controller 中。
我原以为创建一个“CoreDataModel”类来处理与我的堆栈的所有交互并且每个 View Controller 都可以根据需要调用它会更干净(并且是更好的 MVC 实现)。
首先,这看起来合理/可实现吗?
其次,在我的实现中,我将所有核心数据设置代码保留在 AppDelegate 中,并将 managedObjectContext 分配给我的 CoreDataModel 类的一个新实例。但是,在我的第一个 View Controller 中,我调用了我的 CoreDataModel 类的实例方法,称为 (NSMutableArray *)retrieveClientList{}。当方法被正确调用时,我已经设置了一个小的 NSLog 报告,但这似乎没有被调用。
作为帮助,我粘贴了自定义类中的代码,以及 AppDelegate 和第一个 View Controller 。 [在第一个 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;
}
最后,这是我的第一个 View Controller ...
#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]];
}
最佳答案
关于设计的一个评论,我认为这些东西通常作为单例实现(参见此处的大量示例以了解如何创建单例)。
目前,您的模型无法正常工作,因为您没有将核心数据模型对象传递给 View Controller 。创建 VC 后,我希望在您的应用程序委托(delegate)中看到 rootViewController.coreDataModel = coreDataModel
。
关于ios - 将所有核心数据代码整理到自己的类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9332880/