ios - How can I add data to core data using one-to-many relationship? -
i wanting add day week record. have setup entities , relationship within xcode unsure on next step.
am right in thinking need manually set automatically generated foreign key field?
currently save code dat follow:
if let managedobjectcontext = (uiapplication.sharedapplication().delegate as? appdelegate)?.managedobjectcontext{ day = nsentitydescription.insertnewobjectforentityforname("days", inmanagedobjectcontext: managedobjectcontext) as! days day.day = text! day.restday = restday do{ try managedobjectcontext.save() print("saved day sqlite") } catch { print(error) return } }
if auto-generated code model (by letting xcode create subclasses of nsmanagedobject), you're set. need have instance of weeks
available (say, parameter function) when creating days
instance, , add line before saving context:
day.dayweek = week // assuming 'week' existing instance of weeks
this add weeks
instance days
instance, , automatically add days
instance (un)ordered set of days
in same weeks
instance.
Comments
Post a Comment