5
import Foundation
import SwiftUI
import Amplify


class MovesAPI: ObservableObject {
    
    
    @Published var todoLIst: [MoveType?] = [nil]
    
    
    init() {
        listTodos()
    }
    
    func listTodos() -> AnyCancellable { //cannot find type 'AnyCancellable' in scope
        let moveType = MoveType.keys
        let sink = Amplify.API.query(request: .paginatedList(MoveType.self)) //Generic parameter 'R' could not be inferred //Type 'GraphQLRequest<_>' has no member 'paginatedList'
            .resultPublisher
            .sink {
                if case let .failure(error) = $0 {
                    print("Got failed event with error \(error)")
                }
            }
            receiveValue: { result in
            switch result {
                case .success(let todos):
                    print("Successfully retrieved list of todos: \(todos)")
                case .failure(let error):
                    print("Got failed result with \(error.errorDescription)")
                }
            }
        return sink
    }

}

最终我的目标是从 amplify GraphQL API 中获取所有 MoveType 类型的模型。这是直接粘贴到我的项目中的代码片段,我收到了这些类型错误。知道为什么吗?从放大,文档(https://docs.amplify.aws/lib/graphqlapi/query-data/q/platform/ios#query-by-id)我所做的只是更改模型名称以匹配我的后端并删除查询参数。

4

3 回答 3

9

我认为你需要:

import Combine
于 2021-04-16T19:35:54.467 回答
2

当我使用 Cocoapods 下载 Amplify 时,我遇到了来自 amplify 文档的 paginatedList 代码片段无法正常工作的问题。当我按照说明从 Swift Package Manager (SPM) 下载 Amplify 而不是在Amplify iOS Github时,paginatedList 无法正常工作的问题就解决了。将 Combine 框架导入我的 Xcode 项目并没有解决 paginatedList 问题。

于 2021-05-01T03:23:34.600 回答
0

好的,它可以工作,只是需要更简单,我使用组合模式关闭了

import Foundation
import SwiftUI
import Amplify


class MovesAPI: ObservableObject {
    
    
    @Published var todoLIst: [MoveType?] = [nil]
    
    
    init() {
    }
    
    func listTodos() {
        print("listing todos")
    
        Amplify.API.query(request: .list(MoveType.self)) { event in
            switch event {
               case .success(let result):
                   switch result {
                   case .success(let todos):
                       print("Successfully retrieved list of todos: \(todos)")
                   case .failure(let error):
                       print("Got failed result with \(error.errorDescription)")
                   }
               case .failure(let error):
                   print("Got failed event with error \(error)")
               }
           }
    
    }

}

不过,如果有人知道为什么放大文档中的代码片段不起作用,我很想知道。

于 2021-03-13T02:03:11.747 回答