ios - core data - insert new object with two entities -


i'm new core data please bare me if basic question.

i have 2 entities to-many relationship, there can multiple items each list.

  • list
  • item

basically want achieve to save item specific list on click.

i've figured out how save new list:

- (ibaction)handlebtnaddlist:(id)sender  { mylistappdelegate *appdelegate = [[uiapplication sharedapplication] delegate];   nsmanagedobjectcontext *context = [appdelegate managedobjectcontext];   nsmanagedobject *newlist;  newlist =  [nsentitydescription            insertnewobjectforentityforname:@"list"            inmanagedobjectcontext:context]; [newcontact setvalue:@"shopping" forkey:@"name"];  nserror *error; [context save:&error]; } 

but how save item newly created list "shopping" ?

any appreciated. in advance!


hi there, had chance try out. it's working running issue. can add new list, can't seem add item.

i looked in sqlite database , under 'item' entity column named 'zlists' empty. how value in there correspond 'list' item should under?

this i've got

nsmanagedobjectcontext *context = [appdelegate managedobjectcontext]; nsmanagedobject *list = [nsentitydescription                                    insertnewobjectforentityforname:@"list"                                    inmanagedobjectcontext:context]; [list setvalue:@"test list" forkey:@"name"]; nsmanagedobject *item = [nsentitydescription                                       insertnewobjectforentityforname:@"item"                                       inmanagedobjectcontext:context]; [item setvalue:@"test item" forkey:@"name"]; 

i tried adding @ end crashes app

[item setvalue:@"test list" forkey:@"lists"]; 

thanks again time!

for example work code, need these prerequisites...

  1. you have core data model nsmanagedobjectmodel,
  2. within model, have entity list,
  3. within model , entity, have 2 attributes listdate, , listtext,
  4. you have used xcode prepare (or have manually prepared) nsmanagedobject subclass entity list.

instead of line of code: [newcontact setvalue:@"shopping" forkey:@"name"];...

you might choose use dot notation set attribute values in manner...

newlist.listdate = [nsdate date];  //sets attribute current date , time newlist.listtext = <<some user input (for example) view controller>>; 

or match syntax, might choose use key-value coding in manner.

[newlist setvalue:[nsdate date] forkey:@"listdate"]; [newlist setvalue:<<some user input>> forkey:@"listtext"]; 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -