CoreData relationship The development process has many details that need attention. A little careless will be trapped somewhere unable to move.
- Create an entity Contactsdetail (member) and Contactsgroup (group)
- Contactsdetail adding a property and setting the property value type
- Contactsgroup adding a property and setting the property value type
- Contactsdetail add relationship, name. And destination is selected as detail for Contactsgroup,inverse. Delete rule is set to nullify (invalid)
- Contactsgroup, add relationship. Name. And destination is selected as Contactsgroup for Contactsdetail,inverse. Delete rule is set to cascade (concatenation)
- Generate the required files (. h. m) to complete the preparatory work
Start code
Call Method **********************************
Get all Groups
Controllistarray = Databaseclass.dbcontactsgroupreadalldata ()
Insert a group
Databaseclass.dbcontactsgroupinsert (["Name": input!])
Delete a group
Let group = controllistarray! [Indexpath.row] as Contactsgroup
Databaseclass.dbcontactsgroupdeleteobject (Group)
Gets all members of the specified group (to pass in the group entity object)
Controllistarray = Databaseclass.dbcontactsdetailreadalldata (currentgroup!)
Create a member in a specified group
Databaseclass.dbcontactsdetailinsert (group!, datadic: ["name": "Zhang San", "Sex": "Male", "Age": "18"])
Delete a member from a specified group
Let Detaildata = controllistarray! [Indexpath.row] as Contactsdetail
Databaseclass.dbcontactsdetaildeleteobject (Detaildata)
Details notes
- When you create a relationship, selecting inverse may not have a value. To create and name all the relationships of two entities, you can then select each
- Delete the group, but the members are not removed from the database. Delete rule for group relationships must select Cascade (that is, concatenation, delete in series).
Reference
Swift CoreData Relationship