Swift CoreData Relationship

Source: Internet
Author: User

CoreData relationship The development process has many details that need attention. A little careless will be trapped somewhere unable to move.

    1. Create an entity Contactsdetail (member) and Contactsgroup (group)
    2. Contactsdetail adding a property and setting the property value type
    3. Contactsgroup adding a property and setting the property value type
    4. Contactsdetail add relationship, name. And destination is selected as detail for Contactsgroup,inverse. Delete rule is set to nullify (invalid)
    5. Contactsgroup, add relationship. Name. And destination is selected as Contactsgroup for Contactsdetail,inverse. Delete rule is set to cascade (concatenation)
    6. 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

    1. 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
    2. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.