create managed object programmaticly

  • Hi,

    following (partly) the core data low level tutorial I tried created a
    managed object for my custom entity by code.

    It looks ok but crashes in the 2. NSLog.... well maybe because I use
    garbage collection?

    code:
    - (IBAction)saveJobInHistory:(id)sender {
    id destinationObject = [NSEntityDescription
    insertNewObjectForEntityForName:[historyController entityName]
    inManagedObjectContext:[historyController managedObjectContext]];

    if(destinationObject) {
      NSLog(@"got instance of entity");
      NSLog([destinationObject description]);
    }
    }

    historyController is btw an array controller and has the right entity
    set. The context is non nil in any case :)

    Im a core data noob and would be grateful for any advice :)
    Dominik
  • On Dec 18, 2007, at 12:59 PM, Dominik Pich wrote:

    > following (partly) the core data low level tutorial I tried created
    > a managed object for my custom entity by code.
    >
    > It looks ok but crashes in the 2. NSLog.... well maybe because I
    > use garbage collection?
    >
    > code:
    > - (IBAction)saveJobInHistory:(id)sender {
    > id destinationObject = [NSEntityDescription
    > insertNewObjectForEntityForName:[historyController entityName]
    > inManagedObjectContext:[historyController managedObjectContext]];
    >
    > if(destinationObject) {
    > NSLog(@"got instance of entity");
    > NSLog([destinationObject description]);
    > }
    > }

    Do not ever pass an arbitrary string you got from somewhere else as a
    formatting string to printf, NSLog, etc.

    If that string contains formatting characters, you will crash as the
    function will try to read va_args that were not passed and get garbage.

    The correction form is

    NSLog(@"%@", [destinationObject description]);

    Jim
  • Well.. of course you are right - I didnt see the Problem there - and I
    do see that Problem.
    The crash is not because of that though.....

    In gdb
    I cant po  [destinationObject description]
    I can po [destinationObject class] though

    Regards,
    Dominik

    Am Dec 18, 2007 um 7:16 PM schrieb Jim Correia:

    > On Dec 18, 2007, at 12:59 PM, Dominik Pich wrote:
    >
    >> following (partly) the core data low level tutorial I tried created
    >> a managed object for my custom entity by code.
    >>
    >> It looks ok but crashes in the 2. NSLog.... well maybe because I
    >> use garbage collection?
    >>
    >> code:
    >> - (IBAction)saveJobInHistory:(id)sender {
    >> id destinationObject = [NSEntityDescription
    >> insertNewObjectForEntityForName:[historyController entityName]
    >> inManagedObjectContext:[historyController managedObjectContext]];
    >>
    >> if(destinationObject) {
    >> NSLog(@"got instance of entity");
    >> NSLog([destinationObject description]);
    >> }
    >> }
    >
    > Do not ever pass an arbitrary string you got from somewhere else as
    > a formatting string to printf, NSLog, etc.
    >
    > If that string contains formatting characters, you will crash as the
    > function will try to read va_args that were not passed and get
    > garbage.
    >
    > The correction form is
    >
    > NSLog(@"%@", [destinationObject description]);
    >
    > Jim
    >
  • On Dec 18, 2007, at 3:46 PM, Dominik Pich wrote:

    > Well.. of course you are right - I didnt see the Problem there -
    > and I do see that Problem.
    > The crash is not because of that though.....
    >
    > In gdb
    > I cant po  [destinationObject description]
    > I can po [destinationObject class] though

    Then you'll have to provide more information, like the backtrace of
    the crash.

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