Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.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 如何将核心数据添加到我的项目中并存储从JSON服务获取的数据_Ios_Iphone_Json_Core Data_Ios7 - Fatal编程技术网

Ios 如何将核心数据添加到我的项目中并存储从JSON服务获取的数据

Ios 如何将核心数据添加到我的项目中并存储从JSON服务获取的数据,ios,iphone,json,core-data,ios7,Ios,Iphone,Json,Core Data,Ios7,我在appDelegate.h这样做 @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; @property (readonly, strong, nonatomic) NSPersistentStoreCoordi

我在appDelegate.h这样做

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

并在appDelegate.m中实现所有这些东西,这是一个需要处理的相当大的问题,但通常您会向appDelegate.m的
-(BOOL)应用程序:(UIApplication*)应用程序完成启动时使用选项:(NSDictionary*)启动选项

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsDirectory = [[fileManager URLsForDirectory:NSDocumentDirectory
                                                 inDomains:NSUserDomainMask] firstObject];
NSString *documentName = @"YOUR_DATABASE_NAME";
NSURL *url = [documentsDirectory URLByAppendingPathComponent:documentName];
self.document = [[UIManagedDocument alloc] initWithFileURL:url];

BOOL fileExists = [fileManager fileExistsAtPath:[url path]];
if (fileExists) {
    [self.document openWithCompletionHandler:^(BOOL success) {
        if (success) {
            self.context = self.document.managedObjectContext;
            // Post notification so others can gather the document and context
            [[NSNotificationCenter defaultCenter] postNotificationName:@"DatabaseReady"
                                                                object:self];
        }
        if (!success) NSLog(@"couldn't open file at %@", url);
    }];
} else {
    [self.document saveToURL:url
            forSaveOperation:UIDocumentSaveForCreating
           completionHandler:^(BOOL success) {
               if (success) {
                   self.context = self.document.managedObjectContext;
                   // Post notification so others can gather the document and context
                   [[NSNotificationCenter defaultCenter] postNotificationName:@"DatabaseReady"
                                                                       object:self];
               }
               if (!success) NSLog(@"couldn't open file at %@", url);
           }];
}
您应该将
@property(强,非原子)UIManagedDocument*放入文档以便其他视图控制器可以获取该文档。在需要以某种方式联系数据库的视图控制器中,添加以下内容:

- (void)awakeFromNib
{
    // Listen for UIManagedDocument so that we can get the database set up right
    // This only works in awakeFromNib (viewDidLoad is too late)
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(getUIManagedDocument:)
                                                 name:@"DatabaseReady"
                                               object:nil];
}
然后,需要将
getUIManagedDocument:
方法添加到视图控制器:

- (void)getUIManagedDocument:(NSNotification *)notification
{
    self.document = [(OneWorldAppDelegate *)[[UIApplication sharedApplication] delegate] document];
    self.context = self.document.managedObjectContext;
}
当然,您需要添加

@property (strong, nonatomic) UIManagedDocument *document;
@property (strong, nonatomic) NSManagedObjectContext *context;
到您的文件。这就是
self.context
的来源

此时,您已经准备好开始处理数据库了

重要提示:不要忘记在视图控制器中导入appdelegate的.h文件,您要在其中访问appdelegate的@property
文档

至于获取JSON数据,我建议您学习教程,如。它解释得很清楚


希望有帮助

什么是self.contextIt是appdelegate和希望访问数据库的视图控制器都拥有的NSManagedObjectContext public@属性。与核心数据的所有交互都发生在该上下文中。在最初的帖子中,您使用了
@property(只读、强、非原子)NSManagedObjectContext*managedObjectContext而不是我的
上下文
,但这是相同的想法。不管你怎么称呼它。我做了所有这些,但如何从JSON到数据库的数据,这就是为什么我包括教程;这是一项艰巨的任务。完成本教程,主要关注“NSURLSession类套件”一节及以后的内容。如果您对其中的某些内容有疑问,可以在stackoverflow上提问。