Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么委托方法需要保存自定义类托管对象上下文和委托类托管对象上下文的内容?_Ios_Objective C_Core Data_Delegates_Nsmanagedobjectcontext - Fatal编程技术网

Ios 为什么委托方法需要保存自定义类托管对象上下文和委托类托管对象上下文的内容?

Ios 为什么委托方法需要保存自定义类托管对象上下文和委托类托管对象上下文的内容?,ios,objective-c,core-data,delegates,nsmanagedobjectcontext,Ios,Objective C,Core Data,Delegates,Nsmanagedobjectcontext,我为这个奇怪的标题感到抱歉,我是一个初学者,不知道如何问这个问题,所以我会解释自己: 我正在学习核心数据,目前正在学习苹果的示例代码。示例代码用于使用表视图列出书籍 在其AddViewController.h中,声明NSManagedObjectContext,如下所示: #import "DetailViewController.h" @protocol AddViewControllerDelegate; @interface AddViewController : DetailVie

我为这个奇怪的标题感到抱歉,我是一个初学者,不知道如何问这个问题,所以我会解释自己:

我正在学习核心数据,目前正在学习苹果的示例代码。示例代码用于使用表视图列出书籍

在其AddViewController.h中,声明NSManagedObjectContext,如下所示:

#import "DetailViewController.h"

@protocol AddViewControllerDelegate;


@interface AddViewController : DetailViewController 

@property (nonatomic, weak) id <AddViewControllerDelegate> delegate;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;

@end


@protocol AddViewControllerDelegate
- (void)addViewController:(AddViewController *)controller didFinishWithSave:(BOOL)save;
@end
现在,在主表视图屏幕RootViewController中的委托方法中,他们执行以下代码:

- (void)addViewController:(AddViewController *)controller didFinishWithSave:(BOOL)save {

    if (save) {

        NSError *error;
        NSManagedObjectContext *addingManagedObjectContext = [controller managedObjectContext];

        if (![addingManagedObjectContext 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();
        }

        if (![[self.fetchedResultsController managedObjectContext] 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();
        }
    }

    // Dismiss the modal view to return to the main list
    [self dismissViewControllerAnimated:YES completion:nil];
}
我不明白他们为什么要把它保存到AddViewController类的managedObjectContext中。。? 我认为将根视图控制器设置为委托背后的想法是,我们可以在其中执行保存,并传递上下文对象,然后保存它

请帮我在Appdelegate.m中找到它:/

- (NSManagedObjectContext *)managedObjectContextInternal
{
if (__managedObjectContext != nil) {
    return __managedObjectContext;
}

assert([NSThread isMainThread]); // must be creating context on main thread (though it might get read on a background thread from the library importers, but only to create the child contexts)

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
    __managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    __managedObjectContext.undoManager = nil;
    [__managedObjectContext setPersistentStoreCoordinator:coordinator];

    [self debugClearArtwork];
}

return __managedObjectContext;
}
创建或更新后要保存数据时

  [APP_DEGATE saveContext]

在项目中的任何地方,只要使用1 NSManagedObjectContext,就应该调用mainThread。如果不想在后台线程中创建NSManagedObjectContext,这是最佳做法。

AddViewController的NSManagedObjectContext变量来自哪里?它来自AppDelegate,对吗?因为我没有在AddViewController.m中声明-(NSManagedObjectContext*)managedObjectContext,所以我必须从主线程调用它@我不明白。在AddViewController.m中,如何获取NSManagedObjectContext?是从AppDelegate得到的吗?如果是的话,我想一切都好。
  [APP_DEGATE saveContext]