Search code examples
core-datagraphswift3cosmicmind

Is it possibile to update an entity saved into my database with Graph?


i am using the Graph library in order to save my data correctly. I was wondering, if there's a way to update an existing Entity, without duplicate the entity 2 times: user will be allowed to update just two values of the entity. Also, i would like to know if there's a proper save method

I give you an example

let person = Entity(type: "Person")
    person["name"] = //not editable
    person["work"] = //editable
    person["age"]  = //editable
graph.sync() //or something like graph.update

Writing this, i am just creating a new entity, which is not what i want. Maybe, i have to search for the entity, delete that every time, and insert the new one?Hope not.

Thank you for any help you could give!


Solution

  • yes, you can update Entities.

    You need to search for the one you want to update first. For example:

    let graph = Graph()
    let search = Search<Entity>(graph: graph).for(types: "Person")
    for entity in search.sync() {
        // do something
    }
    

    Once you have the entity you want to update, you can set any property, group, or tag as per usual.

    entity["name"] = "Daniel"
    

    From there you need to call graph.sync() or graph.async() so that all is saved.

    Thats it :)