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

  • Home
  • SEARCH
  • 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 7628059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:32:41+00:00 2026-05-31T05:32:41+00:00

I have set up a simple core data schema, where item objects are added

  • 0

I have set up a simple core data schema, where item objects are added to list objects. After generating classes, core data has generated the standard accessors, including a couple of ways to add items to a list (list.addItemsObjects and list.addItems:).

I also want to add a ‘dateAdded’ NSDate to each item. Now, I could manually set this every time I create an object, since core data will provide me with an item.dateAdded accessor. But really, I would prefer that this is handled by the list itself, since the implementation will never change. So every time I call list.AddItemObject, I would like it to set the dateAdded attribute of the item at the same time. I guess I would also need to do the same in the inverse, so that item.setList also sets the dateAdded.

I am not sure how to do this. Should I try to redefine the existing core data-generated accessors? If so, how do I do this (is there someway to call the original implementation inside my custom code, such that the inverse relationship is handled, and any other core data necessities)? Or is there some better way to customize these methods?

  • 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-05-31T05:32:42+00:00Added an answer on May 31, 2026 at 5:32 am

    You can override the core data generated accessors.
    You have to pay attention to a few special things like calling willChangeValueForKey and didChangeValueForKey, but other than that overriding the accessors is pretty much the same as always.

    Read the Apple documentation: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdAccessorMethods.html

    It’s explained clearly with lots of examples. Just pay attention to the difference between one-to-one and one-to-many relations.

    You do not need to implement the inverse functionality, this is done by Core Data. If you call list.AddItem, then item.setList will automatically be called for you.

    So just add your desired code to the item.setList accessor. That way, you guarantee that everytime an item is added to a list the date is updated accordingly. Don’t bother with the list-accessors.

    Your code would be something like:

    - (void)setList:(List *)value
    {
        [self willChangeValueForKey:@"list"];
        [self setPrimitiveValue:[NSDate date] forKey:@"dateAdded"]; // use setValue:forKey: if you need KVO for dateAdded
        [self setPrimitiveValue:value forKey:@"list"];
        [self didChangeValueForKey:@"list"];
    }
    

    EDIT I’ve given it another thought and you might want to know the following:

    If you customize the list methods (ie addItemObject), you’ll also need to customize setItems. Also, if your item is added to a list which happens not to be the list you customized, your custom code is of course not called.

    On the other hand, if you customize the setList method, the code is not called if there are no changes for the item.

    For example, if you call

    [list addItems:[NSSet setWithObject:item]];
    [list addItemsObject:item];
    

    then the item’s setList accessor will only be called once! Calls directly to item.setList are always called, even if nothing changed.

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

Sidebar

Related Questions

I have a Core Data app with a fairly simple data model. I want
My situation is simple: I have some records in my core data store. One
I am fetching a set of objects from a Core Data persistent store using
I have a simple data set, a few collections, not more than 20 documents
I have a fairly simple set of functionality for which I have multiple implementations,
I have a small (500kb) swing applet that displays very simple/limited set of small
I have a very simple .htaccess file set up to redirect a 404 to
I have a simple on my page with the opacity being set to 0.5.
I have a simple website with a master-page. To set properties to elements on
Right so I have a simple app that consists of a calendar a Set

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.