Programmatically create an entity / table using CoreData and paste values ​​into this object in Swift 3 for iOS

I am trying to find a way to programmatically create a database entity / table at run time in Swift 3 for the iOS platform. Please explain the process in detail. I followed other links, but nothing worked for me, and I could not understand the process correctly, so please explain the whole process with working code, and do not provide other existing links.

There is another table added from Xcode, and I can perform database related operations (insert row, delete, update) in this table, but I can’t find a way to perform the following operations on the table that will be created programmatically at runtime . The following operations must be performed.

Add Object - . From the interface, the user can add the name of the entity, the number of columns, column names, and then clicking the button, the object must be created in the database using CoreData. The user can add any number of tables from the interface.

Paste Values . Then the user will have an interface to insert values ​​into the object. The name of the object will be known, and the values ​​for the string in essence will be entered by the user. When a button is clicked, the row should be inserted into the entity / database table.

Extract Values ​​- The user will be able to retrieve all stored values ​​from the object. Use them anytime you need it.

The value of the update . The user will be able to update any value present in the table that was created programmatically.

Delete item . The database entity will also be deleted. When the button is clicked, the name of the object will be transferred to the function, and this entity / table of the database will be deleted from the database.

I tried the following code:

    let model = NSManagedObjectModel()
    let entityName = "TestEntity"
    let entity = NSEntityDescription()
    entity.name = entityName
    entity.managedObjectClassName = entityName

    // Creating a attribute
    let remoteURLAttribute = NSAttributeDescription()
    remoteURLAttribute.name = "columnOne"
    remoteURLAttribute.attributeType = .stringAttributeType
    remoteURLAttribute.isOptional = true
    remoteURLAttribute.isIndexed = true

    var properties = Array<NSAttributeDescription>()
    properties.append(remoteURLAttribute)
    // Adding the attribute to the entity
    entity.properties = properties

    // Adding the entity into the model
    model.entities = [entity]

    do {
        try CoreDataManager.managedObjectContext.save()
    } catch {
        print(error)
    }

    // Inserting the data into the entity
    let testEntity = NSManagedObject(entity: entity, insertInto: CoreDataManager.managedObjectContext)
    testEntity.setValue("WHY SO SERIOUS?", forKey: "columnOne")

    do {
        try CoreDataManager.managedObjectContext.save()
    } catch {
        print(error)
    }

    // Fetching the data from the entity
    let request = NSFetchRequest<NSFetchRequestResult>()
    request.entity = entity
    request.propertiesToFetch = ["columnOne"]
    request.returnsDistinctResults = false
    do {
        let results = try CoreDataManager.managedObjectContext.fetch(request)
        print(results)
    } catch {

    }

-:

▿ 1   - 0: (entity: TestEntity; id: 0x600000422f80  ; : {     columnOne = " ?"; })

, , TestEntity .

+4
1

Core Data. , , . , Core Data.

, NSManagedModel . , , . loadPersistentStores(_:) ( NSPersistentContainer) addPersistentStore(ofType:configurationName:at:options:) ( NSPersistentContainer). , .

Core Data . , .

, - . , Core Data, - , . ( , ). , .

, . Core Data, .

+3

Source: https://habr.com/ru/post/1689355/


All Articles