Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7866693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:28:42+00:00 2026-06-03T00:28:42+00:00

I had spent few hours try to insert/add object but duplicated records happened. Campaign

  • 0

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];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:28:43+00:00Added an answer on June 3, 2026 at 12:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I spent a few days writing a test, then had to add a property
I've been having problems with this code I had spent the last 3 hours
I had a huge problem and spend hours trying to make it work, but
I had spent so many hours failing to find a line graph generator for
Can we add additional methods to jqXHR object? I had a app built using
I spent 2 hours searching through the other few posts at Stack Overflow concerning
First, forgive me if this has been answered already. I spent a few hours
First, I have already spent the past few hours trying to find a solution
Had to download Command Line Tools from Apple to get make to work, but
Had a good read through similar topics but I can't quite a) find one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.