将JSON导入到核心数据中
问题描述:
我正在尝试关注本教程: http://www.raywenderlich.com/12170/core-data-tutorial-how-to-preloadimport-existing-data-updated 在本教程中,将演示如何构建用于创建sqlite并从json导入数据的脚本。 我有写:将JSON导入到核心数据中
static NSManagedObjectModel *managedObjectModel()
{
static NSManagedObjectModel *model = nil;
if (model != nil) {
return model;
}
NSString *path = @"AppChecker";
path = [path stringByDeletingPathExtension];
NSURL *modelURL = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"mom"]];
model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return model;
}
static NSManagedObjectContext *managedObjectContext()
{
static NSManagedObjectContext *context = nil;
if (context != nil) {
return context;
}
@autoreleasepool {
context = [[NSManagedObjectContext alloc] init];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel()];
[context setPersistentStoreCoordinator:coordinator];
NSString *STORE_TYPE = NSSQLiteStoreType;
NSString *path = [[NSProcessInfo processInfo] arguments][0];
path = [path stringByDeletingPathExtension];
NSURL *url = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"sqlite"]];
NSError *error;
NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:STORE_TYPE configuration:nil URL:url options:nil error:&error];
if (newStore == nil) {
NSLog(@"Store Configuration Failure %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
}
}
return context;
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Create the managed object context
NSManagedObjectContext *context = managedObjectContext();
// Custom code here...
// Save the managed object context
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Error while saving %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
exit(1);
}
NSError* err = nil;
NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"brands" ofType:@"json"];
NSArray* Brands = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]
options:kNilOptions
error:&err];
NSLog(@"Imported Brands: %@", Brands);
NSString* dataPath2 = [[NSBundle mainBundle] pathForResource:@"products" ofType:@"json"];
NSArray* Products = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath2]
options:kNilOptions
error:&err];
NSLog(@"Imported Products: %@", Products);
}
return 0;
}
的问题是,它创建.sqlite数据库(和结构是确定),但是没有数据!
我的数据库是这样:
这是我的品牌,例如JSON:
[{
"id":"1",
"name":"TestBrand",
"description":"",
"website":"",
"email":"",
"address":"",
"phone":"",
"from_country_list":"CZ",
"created_at":"2013-11-24 11:51:17.363473",
"updated_at":"2013-11-24 11:51:17.363473"
}]
为什么数据不.sqlite进口任何帮助/提示D b ? 非常感谢。
答
继续本教程。您必须迭代通过JSON文件创建的对象,并将每个实例添加到Core Data对象图中,然后使用可用属性填充它,最后将上下文填充到该对象图中。
只有在最后一步之后才会将数据存储在sqlite数据库中。
+2
大声笑,我需要更多的睡眠...谢谢你(我也在检查数据库等方面失去了很多时间...)。 – user41544
看来你只是通过教程的一半。 JSON数据放入Core Data存储区的部分在代码中完全缺失。在这个教程中是'[Banks enumerateObjectsUsingBlock:..'东西。 –