我在将应用程序委托的上下文传递给视图控制器时遇到了一些问题.我在互联网上找到了很多教程,并建议使用该didFinishLaunchingWithOptions方法创建视图控制器,设置上下文属性并推送它.我的问题是我想使用storyboard,视图控制器是在其中创建和推送的,而不是在app delegate中.
我试图在我的app委托中执行此操作:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//instantiate local context
NSManagedObjectContext *context = [self managedObjectContext];
if (!context)
{
// Handle the error.
NSLog(@"Error: Context is null");
}
//reference the view controller
helloCoreDataViewController1_001 *rootViewController = [helloCoreDataViewController1_001 alloc];
// Pass the managed object context to the view controller
rootViewController.managedObjectContext = context;
return YES;
}
Run Code Online (Sandbox Code Playgroud)
这在我的视图控制器中:
@implementation helloCoreDataViewController1_001
@synthesize name, address, phone, status, managedObjectContext;
//....
- (IBAction)saveContact
{
NSLog(@"name: %@",self.name.text);
NSLog(@"address: %@",self.address.text);
NSLog(@"phone: %@",self.phone.text);
//Save the new instance of …Run Code Online (Sandbox Code Playgroud) 对于父母/子女的情境,我有点困惑ManagedObjectContext.
当我设置子上下文并设置父上下文时,子上下文是否包含父上下文的所有对象?我正在使用在Core Data其中创建的库存方法AppDelegate,但我将其更改ConcurrencyQueue为main.
在我应该更新db的方法中:
我的问题是,它看起来我没有保存任何内容到子上下文.我没有得到更新或创建ChatMessage的println消息.我在这做错了什么?
AppDelegate核心数据方法
lazy var managedObjectContext: NSManagedObjectContext? = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
if coordinator == nil {
return nil
} …Run Code Online (Sandbox Code Playgroud) core-data nsmanagedobjectcontext ios managedobjectcontext swift
问题陈述
当尝试将记录保存到读/写存储(该存储是分配给同一 PersistentStoreCoordinator 的两个 SQLite 存储之一)时,我的 iPhone 应用程序崩溃了。保存记录时的一个明显问题是 PersistentStoreCoordinator 不知道在哪个 Store 中保存数据(只是因为我不知道如何实现这一点)。
首先,我将提供总体情况,以确保我的方法是正确的。然后我将提供实施细节。
背景
这是一个简化的示例,代表了我正在开发的实际应用程序的关键方面。

种子数据

用户输入场景

目前的实施情况
核心数据实施

数据存储和检索

当然,当用户查看选择列表以对属性进行选择时,不应该有任何证据表明这些选择来自两个不同的商店。
持久存储协调器设置
- (NSPersistentStoreCoordinator*)persistentStoreCoordinator {
if (_persistentStoreCoordinator == nil) {
NSArray *bundles = @[[NSBundle bundleForClass:[self class]]];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:bundles]];
NSError *error;
//--------------------------------------------------
// Set options for the USER DATA Persistent Store.
NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES};
//--------------------------------------------------
// Add the USER DATA Store to the Persistent Store Coordinator.
NSPersistentStore *persistentStore = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType …Run Code Online (Sandbox Code Playgroud) sqlite core-data persistent-storage managedobjectcontext ios7