如何从 appdelegate 中降级 rootviewcontroller 以在 Objective C 中定义 NSPersistentContainer

问题描述 投票:0回答:0

我正在尝试按照此处的步骤https://developer.apple.com/documentation/coredata/setting_up_a_core_data_stack?language=objc 使用目标 c 设置核心数据,但它似乎只显示 swift。我拥有的主视图控制器属于

TodoListTableViewController
类,它嵌入在导航控制器中,如下所示:

入口点->导航控制器->TodoListTableViewController

我试着像这样定义我的

AppDelegate::applicationDidFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
        if (self.persistentContainer == nil) {
        [NSException raise:@"did not initialize persistent container in app delegate" format:@"no init"];
    }

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    TodoListTableViewController *todoListVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"TodoListTableViewController"];
    todoListVC.persistentContainer = self.persistentContainer;
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:todoListVC];
    self.window.rootViewController = navController;
    
    return YES;
}

但是我得到了一个丢失的容器异常,因为我在 TodoListTableViewController 中添加了对 persistentContainer 是否存在的检查,如下所示:

- (void)viewDidLoad {
   [super viewDidLoad];
   if (self.persistentContainer == nil) {
     [NSException raise:@"missing container" format:@"missing container"];
   }
}

编辑:下面是 AppDelegate 中的样板核心数据相关代码,用于在选择核心数据时添加 xcode 生成的持久容器和上下文,我正在使用:

@synthesize persistentContainer = _persistentContainer;

- (NSPersistentContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"ToDoList"];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    
                    /*
                     Typical reasons for an error here include:
                     * The parent directory does not exist, cannot be created, or disallows writing.
                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                     * The device is out of space.
                     * The store could not be migrated to the current model version.
                     Check the error message to determine what the actual problem was.
                    */
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
                    abort();
                }
            }];
        }
    }
    
    return _persistentContainer;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *context = self.persistentContainer.viewContext;
    NSError *error = nil;
    if ([context hasChanges] && ![context save:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, error.userInfo);
        abort();
    }
}

以及AppDelegate中xcode包含的头文件:

@property (readonly, strong) NSPersistentContainer *persistentContainer;

谢谢。

ios objective-c core-data
© www.soinside.com 2019 - 2024. All rights reserved.