Note that there are some explanatory texts on larger screens.

plurals
  1. PORestKit and saving to CoreData as NSManagedObject
    primarykey
    data
    text
    <p>I'm using <a href="http://restkit.org/api/latest/" rel="noreferrer">RestKit</a> and i want to parse and save elements to core data. I have two json files:</p> <p>First (Category):</p> <pre><code>[ { "cat_id": 3371, "cat_name": "myName", "image": 762 }, { "cat_id": 3367, "cat_name": "anotherName", "image": 617 } ] </code></pre> <p>And second (Elements):</p> <pre><code>[ { "art_id": "1", "node": { "author": "name" }, "small_url": 0 }, { "art_id": "12", "node": { "author": "anotherName" }, "small_url": 0 } ] </code></pre> <p>So the basic idea is that every category have some elements inside. So this is my CoreData struct: <img src="https://i.stack.imgur.com/iHkHQ.png" alt="enter image description here"></p> <p>I've download the restkit example and use TwitterCoreData sample. My code is: <strong>AppDelegeta.m</strong></p> <pre><code>- (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:&amp;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; } </code></pre> <p>and <strong>ViewController.m</strong>:</p> <pre><code>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); }]; </code></pre> <p>And log for mapping show me the "nil". How to save data from my first json (category) into core data using restkit? Rememnber that i don't have Elements list yet. </p> <p>When i use create new file to create NEManagedObject subclass i've got <strong>Elements</strong> class. </p> <pre><code>@interface Elements : NSManagedObject @property (nonatomic, retain) NSNumber * artId; @property (nonatomic, retain) NSString * author; @property (nonatomic, retain) NSString * title; @property (nonatomic, retain) NSManagedObject *category; @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload