2

I had spent few hours try to insert/add object but duplicated records happened. Campaign record re-inserted even it already exists. Am I missing something?

Below are my codes:

Campaign.h

@interface Campaign : NSManagedObject

@property (nonatomic, strong) NSNumber* campaignId;
@property (nonatomic, strong) NSString* title;

@end

Card.h

@class Campaign;

@interface Card : NSManagedObject

@property (nonatomic, strong) NSNumber* cardId;
@property (nonatomic, strong) NSString* name;
@property (nonatomic, strong) Campaign* campaign;

@end

ViewController.m

...

Campaign* campaign = [Campaign object];
campaign.campaignId = [NSNumber numberWithInt:1];
campaign.title = @"Hello world";

Card* card = [Card object];
card.cardId = @"1234567890";
card.campaign = campaign;

[[[RKObjectManager sharedManager] objectStore] save:nil];

EDITED ViewController.m

...
RKManagedObjectMapping* cardMapping = [RKManagedObjectMapping mappingForClass:[Card class] inManagedObjectStore:[RKObjectManager sharedManager].objectStore];
[cardMapping mapKeyPath:@"id" toAttribute:@"cardId"];
[campaignMapping mapKeyPath:@"name" toAttribute:@"name"];
cardMapping.primaryKeyAttribute = @"cardId";

RKManagedObjectMapping* campaignMapping = [RKManagedObjectMapping mappingForClass:[Campaign class] inManagedObjectStore:[RKObjectManager sharedManager].objectStore];
[campaignMapping mapKeyPath:@"id" toAttribute:@"campaignId"];
[campaignMapping mapKeyPath:@"title" toAttribute:@"title"];
campaignMapping.primaryKeyAttribute = @"campaignId";

Campaign* campaign = [Campaign object];
campaign.campaignId = [NSNumber numberWithInt:1];
campaign.title = @"Hello world";

Card* card = [Card object];
card.cardId = @"1234567890";
card.campaign = campaign;

[[[RKObjectManager sharedManager] objectStore] save:nil];
Teon
  • 155
  • 1
  • 7

1 Answers1

1

Yes, add a .primaryKeyAttribute to your mapping. This will do the pk stuff for you when you are importing data via rest kit. If you are just doing 'normal' core data stuff with Restkit, you need to deal with referential integtrity etc. yourself.

Will Johnston
  • 864
  • 4
  • 18
  • I have updated my code with .primaryKeyAttribute (see edited above) however I still getting duplicated record in campaign entity. It works well with RKObjectManager loadObjectsAtResourcePath but not when I try to add object manually. – Teon Apr 30 '12 at 15:00
  • Yes. If you are just doing core data stuff, you will need to generate ids yourself (if you want them). You don't need them since setting card.campaign will store the relationship, and core data has it's own internal id's that you don't see unless you look in the sqlite db. The only reason you may want them is if you are sync-ing to a remote database and you want to sync with them, but if you do as i said you need to do that yourself in code. – Will Johnston Apr 30 '12 at 15:17
  • Also see http://stackoverflow.com/questions/10264224/auto-index-for-a-coredata-in-objective-c – Will Johnston Apr 30 '12 at 15:46