Search code examples
cocoaperformancecore-datafetchtransient

Core Data - can I store a calculated value as a persistent attribute?


Background

  • I have a calculated value stored in a transient attribute.
  • Every time my app starts up, the transient needs to be updated.
  • Startup time is really slow (i.e. 10-15 seconds)
  • Instruments confirms the update is very expensive.

Question

Is it OK to store a calculated value in a persistent attribute?

I'd use Martin Brugger's Dependant Properties to keep the calculated value up to date.

More Information

My calculated value is the duration on thousands of objects:

Name                       Duration
Users                      70s            
    Proposal.doc           35s   
      12:32-12:32          5s   
      13:11-13:11          30s   
    Proposal2.doc          35s   
      14:32-14:32          15s   
      15:11-15:11          20s   
    ...thousands more objects...

What I've Tried

  • I've minimised the number of faults that are firing, using prefetching and setReturnsObjectsAsFaults:NO but it uses loads of memory and still takes ages.

  • I've made my transient updating code as efficient as I know how.

  • I know I could fetch on a background thread and use a progress bar, but I'd prefer the user to not have to wait at all.


Solution

  • I'm not sure I fully understand the question, but the simple answer to what I think you're asking is "of course." :-)

    There's nothing wrong at all with having a transient, real-time computed "currentTotalDuration" value and a "cachedTotalDuration" attribute. When "currentTotalDuration" is updated, throw it into "cachedTotalDuration" and it's done. It sounds perfectly reasonable to me to have a persistent cached attribute alongside a "live-computed" transient value that's only used when it needs to be updated.

    I'm not familiar with Martin Brugger's Dependant Properties, but it sounds like most of the hard work is already done for you.

    I hope I answered the question you actually asked. :-D