aws appsync offline:获取错误未提供变量ID

时间:2018-05-30 21:27:04

标签: swift offline aws-appsync

我正在使用AWS AppSync进行移动开发(iOS)以实现离线/在线功能

我正在尝试在离线模式下保存数据。但我收到错误"未提供变量ID /缺少值"

当应用程序联机时,它会自动同步到DynamoDB,但问题仅在离线模式下无法获取已保存的记录

以下是应用程序中使用的代码

`

    let userObjInput = userObjectInput(id: "id", firstName: "firstname", lastName: "lastName")

    let CategoryInputs = CreateUserCategoryInput(categoryName: "categoryValue" , user: userObjInput)

    let mutation =  CategoryMutation(input: CategoryInputs)

    appSyncClient?.perform(mutation: mutation, queue: .main, optimisticUpdate: { (transaction) in

            do {
                let selectionSets = try transaction?.read(query: query)
                try transaction?.update(query: GetUserCategoriesOfUserQuery(id: "id")) { (data: inout GetUserCategoriesOfUserQuery.Data) in
                  data.getAllCategoriesForUser?.append(GetUserCategoriesOfUserQuery.Data.GetAllCategoriesForUser?.init(GetUserCategoriesOfUserQuery.Data.GetAllCategoriesForUser.init(id: (UUID().uuidString), categoryName: CategoryInputs.categoryName!, isDeleted: false, user: GetUserCategoriesOfUserQuery.Data.GetAllCategoriesForUser.User?.init(GetUserCategoriesOfUserQuery.Data.GetAllCategoriesForUser.User.init(id: userObjInput.id!, firstName: userObjInput.firstName!, lastName: userObjInput.lastName!)))))
            } catch {
                print(error.localizedDescription)
           }
    }, conflictResolutionBlock: nil, resultHandler: { (result, error) in

        if error == nil {
            fetchCategories()

        } else {
            print(error?.localizedDescription)
        }
    })`

1 个答案:

答案 0 :(得分:0)

对于optimistic UI missing value有疑问的人。我发现了一种方法,可以通过使用客户端应用程序中的Custom Request Header传递参数来临时解决问题。
以前,您的查询看起来像这样allDiaries(author: String): [Diary]
只需将其更改为=> allDiaries: [Diary]
因此您的request mapping如下所示:

{
    "version" : "2017-02-28",
    "operation" : "Scan",
    "filter" : {
        "expression" : "author = :author",
        "expressionValues" : {
            ":author" : { "S" : "$context.request.headers.author" }
        }
    }
}

参考:How to pass AWS AppSync custom request header in iOS client?
希望它有用!好运:)