Core Data - awakeFromInsert

  • I am trying to implement defaults by creating a core data object to
    hold these defaults.  This way they do not reset to zero when the
    program is run from a different user or a different machine as long
    as they have access to the same core data file.  Two entries are
    created using the following code and I don't know why.  It seems to
    occur at executeFetchRequest.
    Thank You.

    #import "Customer.h"

    @implementation Customer

    -(void) awakeFromInsert
    {

        // The following works for incrementing ID's for "same user" on
    "same machine", it resets to zero for different user or machine. ONLY
    ONE ENTRY CREATED

        //    long value = [[[NSUserDefaults standardUserDefaults]
    objectForKey: @"recordID"] longValue];
        //    [[NSUserDefaults standardUserDefaults] setValue:[NSNumber
    numberWithLong:(value + 1)] forKey:@"recordID"];
        //    [self setPrimitiveValue:[NSNumber numberWithLongLong:
    (value)] forKey:@"recordID"];

        // The following works for incrementing ID's for "any user" on
    "any machine", but it creates 2 entries each time.

        NSFetchRequest *request = [[[NSFetchRequest alloc] init]
    autorelease];
        NSEntityDescription *entity = [[[[NSApp delegate]
    managedObjectModel] entitiesByName] objectForKey:@"Defaults"];
        [request setEntity:entity];

        NSManagedObjectContext *context = [[NSApp delegate]
    managedObjectContext];

        // If I comment out everything below only one entry is created.

        NSArray *myArray = [context executeFetchRequest:request
    error:nil]; // This line creates the problem.
        id masterRecord = [myArray objectAtIndex:0];

        long value = [[masterRecord valueForKeyPath: @"recordID"]
    longValue];

        [masterRecord willChangeValueForKey: @"recordID"];
        [masterRecord setPrimitiveValue:[NSNumber numberWithLongLong:
    (value + 1)] forKey:@"recordID"];
        [masterRecord didChangeValueForKey: @"recordID"];

        [self setPrimitiveValue:[NSNumber numberWithLongLong:(value)]
    forKey:@"recordID"];

    }

    @end
  • On Oct 4, 2005, at 4:43 PM, Jim Murry wrote:

    > I am trying to implement defaults by creating a core data object to
    > hold these defaults.
    >
    It's not clear why you're trying to do this (in this way)...

    > This way they do not reset to zero when the program is run from a
    > different user or a different machine as long as they have access
    > to the same core data file.

    ... you can establish "factory settings" (NSRegistrationDomain) for
    user defaults much more easily (see <http://developer.apple.com/
    documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/
    NSUserDefaults.html#//apple_ref/doc/uid/20000318-CIHDDCDB
    >).

    What are you actually trying to achieve?

    > [masterRecord willChangeValueForKey: @"recordID"];
    > [masterRecord setPrimitiveValue:[NSNumber numberWithLongLong:
    > (value + 1)] forKey:@"recordID"];
    > [masterRecord didChangeValueForKey: @"recordID"];
    > [self setPrimitiveValue:[NSNumber numberWithLongLong:(value)]
    > forKey:@"recordID"];
    >
    Don't do this!
    Either use the standard KVO-compliant accessors or use the primitive
    accessors.  It's not clear what you're trying to achieve doing this,
    or why.

    mmalc
  • This was just a simple version to describe the problem I was having

    On Oct 4, 2005, at 6:04 PM, mmalcolm crawford wrote:

    >
    > On Oct 4, 2005, at 4:43 PM, Jim Murry wrote:
    >
    >
    >> I am trying to implement defaults by creating a core data object
    >> to hold these defaults.
    >>
    >>
    > It's not clear why you're trying to do this (in this way)...

    I was just trying to take a value from an entity "Defaults" as
    masterRecord.recordID then and set the recordID in a newly created
    entity "Customer".  Maybe "Defaults" was not the best choice for the
    entity name in the example.  I am just trying to use value from a
    fetched record to set value in a new record sometimes in the same
    entity.

    When I perform a fetch within awakeFromInsert I get two entries
    showing up in the array.

    >
    >> This way they do not reset to zero when the program is run from a
    >> different user or a different machine as long as they have access
    >> to the same core data file.
    >>
    >
    > ... you can establish "factory settings" (NSRegistrationDomain) for
    > user defaults much more easily (see <http://developer.apple.com/
    > documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/
    > NSUserDefaults.html#//apple_ref/doc/uid/20000318-CIHDDCDB>).
    >
    >
    > What are you actually trying to achieve?
    >
    >
    >
    >> [masterRecord willChangeValueForKey: @"recordID"];
    >> [masterRecord setPrimitiveValue:[NSNumber numberWithLongLong:
    >> (value + 1)] forKey:@"recordID"];
    >> [masterRecord didChangeValueForKey: @"recordID"];
    >> [self setPrimitiveValue:[NSNumber numberWithLongLong:(value)]
    >> forKey:@"recordID"];
    >>
    >>
    > Don't do this!
    > Either use the standard KVO-compliant accessors or use the
    > primitive accessors.  It's not clear what you're trying to achieve
    > doing this, or why.

        [masterRecord willChangeValueForKey:
    @"recordID"];
                                  //  I did this so the next line would
    persist

        [masterRecord setPrimitiveValue:[NSNumber numberWithLongLong:
    (value + 1)] forKey:@"recordID"];  //    is this wrong?  This is a
    fetched record and it


                                                    //    would not
    persist without the other lines.
        [masterRecord didChangeValueForKey:
    @"recordID"];
                                  //    I did this so previous line would
    persist

        [self setPrimitiveValue:[NSNumber numberWithLongLong:(value)]
    forKey:@"recordID"];                            //    This is where
    I was setting recordID in the


                                                    //    newly created
    record for the value fetched


                                                    //    from a
    different record, is this line wrong as


                                                    //    well?

    Jim
previous month october 2005 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
31            
Go to today