Ios 如何将核心数据添加到我的项目中并存储从JSON服务获取的数据
我在appDelegate.h这样做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
@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*放入文档在appdelegate的.h文件中编写>以便其他视图控制器可以获取该文档。在需要以某种方式联系数据库的视图控制器中,添加以下内容:
- (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上提问。