PDA

View Full Version : insertNewObjectForEntityForName




KieferThomas
Mar 4, 2013, 08:51 PM
Hello all,

I am using the following code in a project to insert a new record from a button click:

NSManagedObject *Entry = Entry = [NSEntityDescription insertNewObjectForEntityForName:@"Entity"
inManagedObjectContext:context];

This code is working to add a new record to my database when I click my button.

It is not moving to the new record for editing.

I do not know how to explain this better then I have other then for the fact of after the record is inserted into the database I physically have to click on my table or use my move next or move previous buttons to get to the new record... The new record is not moved to automatically to start editing...

Thanks,
Kiefer



Sonnestah
Mar 4, 2013, 10:25 PM
I dont get what you mean by moving to the new record for editing, but something that is just weird is

NSManagedObject *Entry = Entry =

This doesnt make sense, just use Entry once:

NSManagedObject *Entry = [NSEntityDescription insertNewObjectForEntityForName:@"Entity"
inManagedObjectContext:context];

KieferThomas
Mar 5, 2013, 07:07 AM
Thanks for your response Sonnestah,

What I am speaking of: "moving to the new record" is I have to physically click on my table to get to the new record so I can enter my information in the empty fields. The above code does not clear my fields and set the new record as active.

My goal with the code is as if I had linked my button in IB, right clicking and connecting to the ArrayController and making the Add connection. When I do this a new record is inserted and the first field is selected for input into the new record.

The reason I am not doing the a fore mentioned is I am needing control over the newly inserted record for inserting date/time and setting focus to a specific field to continue editing.

The help with getting the new record active is what I need help with.

The point you picked out was corrected. I am not sure why the program didn't crash on compile. But with the correction the program is still doing the same as it was before.

Thanks,
Keith

cqexbesd
Mar 5, 2013, 10:15 AM
I am not sure why the program didn't crash on compile. But with the correction the program is still doing the same as it was before.

It compiled because it is a valid expression. It will be evaluated from right to left so whilst redundant the extra assignment doesn't have any effect.

So the code becomes, more or less:

Entry = [NSEntityDescription insertNewObjectForEntityForName:@"Entity"
inManagedObjectContext:context];
Entry = Entry;

ArtOfWarfare
Mar 5, 2013, 11:10 AM
It compiled because it is a valid expression. It will be evaluated from right to left so whilst redundant the extra assignment doesn't have any effect.

So the code becomes, more or less:

Entry = [NSEntityDescription insertNewObjectForEntityForName:@"Entity"
inManagedObjectContext:context];
Entry = Entry;

But the variable type declaration was on the far left...

cqexbesd
Mar 17, 2013, 12:49 PM
But the variable type declaration was on the far left...

Declaring a variable tells the compiler you need a bit of space to store some data. That is read at compile time and whatever needs setting up is set up. A variable declaration doesn't become a series of assembler instructions in the way an operation or function call does. Therefore the variable exists (i.e. some memory is reserved to store it), often from the moment the function is entered, but at least from the place where you declared it - certainly before your code that accesses it is run.