Adding/removing managed objects: how to know the maximum index?

  • My Core Data managed objects (instances) all have their own index. Naturally, when I add an object, I want to set its index to the highest index value so far, + 1. This should be done in - awakeFromInsert.
    However, when I perform a fetch request from there, a second (new) item appears in the table (instead of just one). This can't be right.

    So, the question is how to keep track of the maximum index in a correct way.
    Should I maintain a class variable for the maximum index (updated on awakeFromFetch / -Insert)? If so, how do I keep track of removed objects (say when the object with the highest index gets removed)?

    Suggestions appreciated.


    Best regards,
    Arthur C.
  • On 14-Nov-07, at 3:47 PM, Arthur C. wrote:

    > My Core Data managed objects (instances) all have their own index.
    > Naturally, when I add an object, I want to set its index to the
    > highest index value so far, + 1. This should be done in -
    > awakeFromInsert.
    > However, when I perform a fetch request from there, a second (new)
    > item appears in the table (instead of just one). This can't be right.
    >
    > So, the question is how to keep track of the maximum index in a
    > correct way.
    > Should I maintain a class variable for the maximum index (updated
    > on awakeFromFetch / -Insert)? If so, how do I keep track of removed
    > objects (say when the object with the highest index gets removed)?
    >
    > Suggestions appreciated.

    Arthur,

    I would add to the data model an entity (say Info) to maintain the
    maximum index.  If I didn't need my object indices to be contiguous
    then I would simply increment the index on insertion and do nothing
    on deletion (rather than updating the indices of the remaining
    objects and decrementing the maximum index).  If I didn't mind having
    gaps in the indices but wanted to reuse them when possible then I
    would add a set of unused indices as a to-many relation of the Info
    entity.  These properties could be transient, but I think its much
    simpler to make them persistent.

    Cheers,
    dave

    > Best regards,
    > Arthur C.
  • On 14-Nov-07, at 3:47 PM, Arthur C. wrote:

    > My Core Data managed objects (instances) all have their own index.
    > Naturally, when I add an object, I want to set its index to the
    > highest index value so far, + 1. This should be done in -
    > awakeFromInsert.
    > However, when I perform a fetch request from there, a second (new)
    > item appears in the table (instead of just one). This can't be right.
    >
    > So, the question is how to keep track of the maximum index in a
    > correct way.
    > Should I maintain a class variable for the maximum index (updated
    > on awakeFromFetch / -Insert)? If so, how do I keep track of removed
    > objects (say when the object with the highest index gets removed)?
    >
    > Suggestions appreciated.

    Arthur,

    I would add to the data model an entity (say Info) to maintain the
    maximum index.  If I didn't need my object indices to be contiguous
    then I would simply increment the index on insertion and do nothing
    on deletion (rather than updating the indices of the remaining
    objects and decrementing the maximum index).  If I didn't mind having
    gaps in the indices but wanted to reuse them when possible then I
    would add a set of unused indices as a to-many relation of the Info
    entity.  These properties could be transient, but I think its much
    simpler to make them persistent.

    Cheers,
    dave

    > Best regards,
    > Arthur C.
  • On 14-Nov-07, at 3:47 PM, Arthur C. wrote:

    > My Core Data managed objects (instances) all have their own index.
    > Naturally, when I add an object, I want to set its index to the
    > highest index value so far, + 1. This should be done in -
    > awakeFromInsert.
    > However, when I perform a fetch request from there, a second (new)
    > item appears in the table (instead of just one). This can't be right.
    >
    > So, the question is how to keep track of the maximum index in a
    > correct way.
    > Should I maintain a class variable for the maximum index (updated
    > on awakeFromFetch / -Insert)? If so, how do I keep track of removed
    > objects (say when the object with the highest index gets removed)?
    >
    > Suggestions appreciated.

    Arthur,

    I would add to the data model an entity (say Info) to maintain the
    maximum index.  If I didn't need my object indices to be contiguous
    then I would simply increment the index on insertion and do nothing
    on deletion (rather than updating the indices of the remaining
    objects and decrementing the maximum index).  If I didn't mind having
    gaps in the indices but wanted to reuse them when possible then I
    would add a set of unused indices as a to-many relation of the Info
    entity.  These properties could be transient, but I think its much
    simpler to make them persistent.

    Cheers,
    dave

    > Best regards,
    > Arthur C.
  • On Nov 14, 2007, at 5:47 PM, Arthur C. wrote:

    > My Core Data managed objects (instances) all have their own index.
    > Naturally, when I add an object, I want to set its index to the
    > highest index value so far, + 1. This should be done in -
    > awakeFromInsert.
    > However, when I perform a fetch request from there, a second (new)
    > item appears in the table (instead of just one). This can't be right.

    See the documentation at:

    <http://developer.apple.com/documentation/Cocoa/Reference/CoreDataFramework/
    Classes/NSManagedObject_Class/Reference/NSManagedObject.html
    >

    See the section "Methods to Override Considerations". -
    initWithEntity:insertIntoManagedObjectContext: is listed as a method
    which cannot have side effects on the context; you cannot fetch or
    save from it. -awakeFromInsert is called from in that same call stack,
    so fetching from awakeFromInsert is probably also not allowed.

    I've filed a bug asking for doc clarification.

    Jim
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today