Note that there are some explanatory texts on larger screens.

plurals
  1. PORestKit many to many relationship saves new rows in both join table and null values in main table
    primarykey
    data
    text
    <p>I use RestKit to cache data from a remote server locally. In it I have a many to many relationship between Category &lt;&lt;-->> News. The mapping seems to work properly, despite that it also saves null values in my Category table (it saves the correct categories too). Like the image below:</p> <p><img src="https://i.stack.imgur.com/1WSdP.png" alt="enter image description here"></p> <p>It seems to save 30 null rows, I also have 30 (not null) rows in my join table so there might be a correlation here. </p> <p>The JSON that I get looks like this: <code>"categories":[{"category_id":1},{"category_id":4}]</code></p> <p>I have two custom model objects that inherits from <code>NSManagedObject</code>. </p> <pre><code>@interface News : NSManagedObject [...] @property (nonatomic, retain) NSSet *categories; @end @interface Category : NSManagedObject [...] @property (nonatomic, retain) NSSet *news; @end </code></pre> <p>I use <code>@dynamic</code> on both. </p> <p>My mappings looks like this: </p> <pre><code>RKManagedObjectMapping *categoryMapping = [RKManagedObjectMapping mappingForClass:[Category class] inManagedObjectStore:objectManager.objectStore]; categoryMapping.primaryKeyAttribute = @"categoryId"; categoryMapping.rootKeyPath = @"categories"; [categoryMapping mapKeyPath:@"id" toAttribute:@"categoryId"]; [...] RKManagedObjectMapping* newsMapping = [RKManagedObjectMapping mappingForClass:[News class] inManagedObjectStore:objectManager.objectStore]; newsMapping.primaryKeyAttribute = @"newsId"; newsMapping.rootKeyPath = @"news"; [newsMapping mapKeyPath:@"id" toAttribute:@"newsId"]; [...] // Categories many-to-many (not fully working yet). [newsMapping mapKeyPath:@"categories" toRelationship:@"categories" withMapping: categoryMapping]; // Register the mappings with the provider [objectManager.mappingProvider setObjectMapping:newsMapping forResourcePathPattern:@"[path-to-JSON]"]; [objectManager.mappingProvider setObjectMapping:categoryMapping forResourcePathPattern:@"[path-to-JSON]"]; </code></pre> <p>I fetch the data like this (much like the Twitter RestKit example):</p> <pre><code>- (id)init { self = [super init]; if (self) { [self loadCategories]; [self loadCategoriesFromDataStore]; [self loadNews]; [self loadNewsFromDataStore]; } return self; } - (void)loadCategoriesFromDataStore { NSFetchRequest* request = [Category fetchRequest]; NSSortDescriptor* descriptor = [NSSortDescriptor sortDescriptorWithKey:@"categoryId" ascending:YES]; [request setSortDescriptors:[NSArray arrayWithObject:descriptor]]; _categories = [Category objectsWithFetchRequest:request]; } - (void)loadNewsFromDataStore { NSFetchRequest* request = [News fetchRequest]; NSSortDescriptor* descriptor = [NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:NO]; [request setSortDescriptors:[NSArray arrayWithObject:descriptor]]; _news = [News objectsWithFetchRequest:request]; } - (void)loadCategories { // Load the object model via RestKit RKObjectManager *objectManager = [RKObjectManager sharedManager]; [objectManager loadObjectsAtResourcePath:@"[link-to-JSON]" delegate:self]; } - (void)loadNews { // Load the object model via RestKit RKObjectManager *objectManager = [RKObjectManager sharedManager]; [objectManager loadObjectsAtResourcePath:@"[link-to-JSON]" delegate:self]; } - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects { [[NSUserDefaults standardUserDefaults] synchronize]; [self loadCategoriesFromDataStore]; [self loadNewsFromDataStore]; } </code></pre> <p>Any ideas what I'm doing wrong? </p> <p><strong>Note:</strong> It also seems to save 30 rows to the join table when there are 15 News, I don't know if this is the normal behavior for join tables. </p> <p><strong>Update:</strong> It also maps the the wrong category id's in the join table (ie. category 66, a null row). </p> <p><strong>update 2:</strong> JSON get request for my Category {"categories":{[...], "id":1, [...]}</p>
    singulars
    1. This table or related slice is empty.
    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.
    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