我正在使用RestKit,我想解析元素并将其保存到核心数据。我有两个 json 文件:
第一(类别):
[
{
"cat_id": 3371,
"cat_name": "myName",
"image": 762
},
{
"cat_id": 3367,
"cat_name": "anotherName",
"image": 617
}
]
第二个(元素):
[
{
"art_id": "1",
"node": {
"author": "name"
},
"small_url": 0
},
{
"art_id": "12",
"node": {
"author": "anotherName"
},
"small_url": 0
}
]
所以基本思想是每个类别里面都有一些元素。所以这是我的 CoreData 结构:
我已经下载了 restkit 示例并使用 TwitterCoreData 示例。我的代码是: AppDelegeta.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *baseURL = [NSURL URLWithString:@"http://globalURL.com"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
RKEntityMapping *categoryMapping = [RKEntityMapping mappingForEntityForName:@"Category" inManagedObjectStore:managedObjectStore];
categoryMapping.identificationAttributes = @[ @"catId" ];
[categoryMapping addAttributeMappingsFromDictionary:@{
@"cat_id": @"catId",
@"node.author": @"author",
}];
RKEntityMapping *elementsMapping = [RKEntityMapping mappingForEntityForName:@"Elements" inManagedObjectStore:managedObjectStore];
elementsMapping.identificationAttributes = @[ @"artId" ];
[elementsMapping addAttributeMappingsFromDictionary:@{
@"art_id": @"artId",
@"node.author": @"author",
}];
[elementsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"category" toKeyPath:@"category" withMapping:categoryMapping]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:elementsMapping
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"MyCoreData.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"MyCoreData" ofType:@"sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];
// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
和ViewController.m:
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Elements"];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"artId" ascending:NO];
fetchRequest.sortDescriptors = @[descriptor];
[[RKObjectManager sharedManager] getObjectsAtPath:@"/detailaddress/:catId" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load complete: Table should refresh...");
NSLog(@"%@",mappingResult);
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"LastUpdatedAt"];
[[NSUserDefaults standardUserDefaults] synchronize];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Load failed with error: %@", error);
}];
映射日志向我显示“nil”。如何使用restkit将我的第一个json(类别)中的数据保存到核心数据中?请记住,我还没有元素列表。
当我使用创建新文件来创建 NEManagedObject 子类时,我有Elements类。
@interface Elements : NSManagedObject
@property (nonatomic, retain) NSNumber * artId;
@property (nonatomic, retain) NSString * author;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSManagedObject *category;
@end