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 6814783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:43:45+00:00 2026-05-26T20:43:45+00:00

Say I have an NSManagedObject subclass called Item . Whenever an item instance is

  • 0

Say I have an NSManagedObject subclass called Item. Whenever an item instance is saved I want to update a property based on a computed value from a transient property. I know I can update the property whenever the transient property changes but for this question, assume there’s a good reason I don’t want to do that.

I tried to do this in the willSave method as follows:

- (void)willSave
{
    self.computedProperty = [self computedValueFromTransientProperty];
}

This causes a crash when saving the context. If I move the code out of willSave and set the property explicitly before invoking save it works fine. The apple docs say you should avoid changing managed object properties in willSave.

QUESTION: is there a good way to build functionality into a NSManagedObject subclass so a property can be updated just before save w/o having to explicitly set the property from outside the class and w/o setting the property every time the transient property changes?

  • 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-26T20:43:46+00:00Added an answer on May 26, 2026 at 8:43 pm

    You’re able to set persistent properties from willSave, you just have to be more careful about it.

    From the willSave docs:

    This method can have “side effects” on persistent values. You can use
    it to, for example, compute persistent values from other transient or
    scratchpad values.

    If you want to update a persistent property value, you should
    typically test for equality of any new value with the existing value
    before making a change. If you change property values using standard
    accessor methods, Core Data will observe the resultant change
    notification and so invoke willSave again before saving the object’s
    managed object context. If you continue to modify a value in willSave,
    willSave will continue to be called until your program crashes.

    So, what’s happening is you’re changing computedProperty, which is causing willSave to be called again, which changes computedProperty which calls willSave again, until your program crashes.

    To fix this you need to check whether or not computedProperty needs to be set again:

    - (void)willSave
    {
        id computed = [self computedValueFromTransientProperty];
        if (![self.computedProperty isEqual:computed])
        {
            self.computedProperty = computed;
        }
    }
    

    This will mean that computedValueFromTransientProperty will get called twice, so you might not want to do this if the method is computationally expensive.

    Another option is to use the primitive set method which will mean willSave won’t get called twice, but might have side affects depending on how your app interacts with Core Data:

    - (void)willSave
    {
        self.primitiveComputedProperty = [self computedValueFromTransientProperty];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a custom NSManagedObject Department and this has a property representing a
I have a custom NSManagedObject subclass, say, Person . I also have a UIView
Say I have three files (template_*.txt): template_x.txt template_y.txt template_z.txt I want to copy them
Let's say have a string... String myString = my*big*string*needs*parsing; All I want is to
Say have a linear model LM that I want a qq plot of the
Say I have a file called hello.txt which contains Hello World!. If I wanted
Say I have a bunch of objects with dates and I regularly want to
Say I have a GUI, and I want the program to actually run when
Say have normal class in iOS, called A. I'd like to pass this as
Say I have a method that returns this. Vector[ (PkgLine, Tree) ]() I want

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.