私はRestKitを使用しており、要素を解析してコア データに保存したいと考えています。2 つの json ファイルがあります。
最初 (カテゴリ):
[
{
"cat_id": 3371,
"cat_name": "myName",
"image": 762
},
{
"cat_id": 3367,
"cat_name": "anotherName",
"image": 617
}
]
そして2番目(要素):
[
{
"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 (カテゴリ) からコアデータにデータを保存するにはどうすればよいですか? 私はまだ要素リストを持っていないことを思い出してください。
create new file を使用して 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