0

I’m using AWS AppSync for developping iOS App by Swift. And so I record Data to AWS DynamoDB.
I can use GraphQL Operarion 「Create~~Mutation」「Update~~Mutation」「Delete~~Mutation」 so I can confirm Data created, updated, deleted. To be accurate, Data deleted disappered from DynamoDB Console, I can check it. Well, the problem is that when I Query Data updated , I can only Query Data BEFORE Updated.

// schema.GraphQL
type Todo @model {
  id: ID!
  name: String!
  description: String
}


@IBOutlet weak var idText: UITextField!
@IBOutlet weak var nameText: UITextField!
@IBOutlet weak var desText: UITextField!

 @IBAction func Update(_ sender: Any) {

        let up = UpdateTodoInput.init(id: idText.text!, name: nameText.text!, description: desText.text)

        appSyncClient?.perform(mutation: UpdateTodoMutation(input: up)) { (result, error) in
            if let error = error as? AWSAppSyncClientError {
                print("Error occurred: \(error.localizedDescription )")
            }
            if let resultError = result?.errors {
                print("Error saving the item on server: \(resultError)")
                return
            }
            print(result?.data?.updateTodo)
        }

    }


 @IBAction func Get(_ sender: Any) {

        let get = GetTodoQuery.init(id: idText.text!)

        appSyncClient?.fetch(query: get)  { (result, error) in
            if error != nil {
                print(error?.localizedDescription ?? "")
                return
            }
            print(result?.data?.getTodo)
            self.did = result?.data?.getTodo
            print(self.did)


            DispatchQueue.main.async {
                self.nameText.text! = result?.data?.getTodo?.name as! String
                self.desText.text! = result?.data?.getTodo?.description as! String
            }

        }


    }

〜preamble〜

propaty I want to update can be updated by entering the value to 「idText」「nameText」「desText」

when I run Get(_ sender: Any), I can get data from DynamoDB using Table UNIQUE ID.

And Succeed in Getting Data, 「nameText」「desText」, both of UITextFiled.text property is change.

Getting Data's name,description is set to nameText.text,desText.text.

〜preamble〜

when I run Update(_ sender: Any) to existing data.

I can confirm updated Data by DynamoDB Console, then I run Get(_ sender: Any) to the

Data, that is Getting UPDATE Data! but I can only get UNUPDATED Data!

for example, Create this Data↓

CreateTodoInput.init(id: "123", name: "daigo", description: "ichikawa")

And I'm ganna update Created data this by Update(_ sender: Any)

idText.text = "123"
nameText.text = "Jack"
desText.text = "Sparo"

UpdateTodoInput.init(id: idText.text!, name: nameText.text!, description: desText.text)

then I will Get updated Data by Get(_ sender: Any)

idText.text = "123"

GetTodoQuery.init(id: idText.text!)

I can only get this

(id: "123", name: "daigo", description: "ichikawa")

but I checked AWS DynamoDB Console, Updated Data is represented. why is this happen?

1 Answers1

0

I got it !

You should not forget this argument!

cachePolicy: .returnCacheDataAndFetch

You can Get Updated Data by following this Code!

appSyncClient?.fetch(query: post, cachePolicy: .returnCacheDataAndFetch)  { (result, error) in
if error != nil {
print(error?.localizedDescription ?? "")
return
}
}

Thank You!