我有一个基于Tim Roadlys Tutorial的coredata工作应用程序。该应用程序使用标签栏,一个加载coredata。我有另一个加载UIView
的标签,然后我通过一个按钮链接到另一个tableView
,它也会有一个coredata。
问题是当您单击按钮时第二个视图崩溃。我是否通过segue传递托管对象上下文,还是必须将其放入appdelegate
?
感谢您的帮助:)
这是segue
-(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:无 cacheName:无];
错误是 - NSFetchedResultsController
的实例需要非零[{1}}和fetchRequest
答案 0 :(得分:0)
这不是很多信息和零代码,但我会把它放在名为
的prepareForSegue中答案 1 :(得分:0)
为什么不创建一个单独的单例类来保存MOC并使用两个控制器而不是在控制器中创建MOC?
答案 2 :(得分: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];
}