【发布时间】:2020-08-01 03:48:15
【问题描述】:
我使用的是 xcode 11.4 和 swift4,我目前正在使用 AWS GraphQL 并学习正确的工作流程。我的amplify.xyz 配置设置为
push=true
modelgen=true
profile=default
envName=amplify
以便模型在创建/编辑时生成。在schema.graphql我定义用户:
type User @model {
id: ID!
firstName : String!
lastName : String!
handle : String!
email : String!
}
并构建/运行应用程序,并能够按预期创建/读取user 的实例。然后假设我添加了一个简单的新字段User @model,这样我就有了:
type User @model {
id: ID!
firstName : String!
lastName : String!
handle : String!
email : String!
blank : String!
}
然后清理构建文件夹,并重新构建应用程序。然后我得到莫名其妙的错误
No such module 'Amplify' HomeController.swift
即使更改 Model 类和 Amplify 似乎无关。如果我删除blank,然后清理并重建,一切都会恢复正常。这种行为的原因是什么?
作为参考,这是我的 podfile:
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'alpha' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for alpha
pod 'amplify-tools'
pod 'Amplify'
pod 'AWSPluginsCore'
pod 'AmplifyPlugins/AWSAPIPlugin'
pod 'AWSMobileClient', '~> 2.13.0' # Required dependency
pod 'AWSUserPoolsSignIn', '~> 2.13.0'
pod 'AWSAppSync', '~> 3.1.0'
pod 'AWSMobileClient', '~> 2.13.0'
pod 'AWSAuthUI', '~> 2.13.0'
pod 'AWSUserPoolsSignIn', '~> 2.13.0'
end
______________________ 更新 ___________________
按照 Julien S 的建议,我是 amplify push,并确保 amplify/generated/models 中的所有文件都移动到每个 (https://aws-amplify.github.io/docs/ios/start?ref=amplify-iOS-btn) 的顶级目录中。现在这个问题No such module 'Amplify' HomeController.swift 解决了。但是,我再也找不到模型更新之前保存的数据。作为参考,当用户创建帐户时,我会访问用户的令牌并将其与用户的电子邮件一起保存。然后下次用户打开应用程序时,我再次获取令牌并通过令牌查询用户数据库。相关代码:
class CognitoPoolProvider : AWSCognitoUserPoolsAuthProviderAsync {
func getLatestAuthToken(_ callback: @escaping (String?, Error?) -> Void) {
AWSMobileClient.default().getTokens { (token, error) in
if let error = error {
callback(nil,error)
}
callback(token?.accessToken?.tokenString, error)
}
}
}
在 MainController.swift 中:
override func viewDidLoad() {
super.viewDidLoad()
// get user token
let pool = CognitoPoolProvider();
pool.getLatestAuthToken { (token, error) in
if let error = error {
print("error: \(error)")
} else {
self.getUserData(token: token!)
}
}
}
func getUserData(token:String){
print("token >>>> \(token)")
// this is successful. you got all the user stuff
// when you change the user model, you can no longer query the user
let _ = Amplify.API.query(from: User.self, byId: token) { (event) in
switch event {
case .completed(let result):
switch result {
case .success(let note):
guard let note = note else {
print("API Query completed but missing user")
return
}
print("API Query successful, got user: \(note)")
case .failure(let error):
print("Completed with error: \(error.errorDescription)")
}
case .failed(let error):
print("Failed with error \(error.errorDescription)")
default:
print("Unexpected event")
}
}
}
【问题讨论】:
-
不要使用访问令牌作为标识符。它一直在变化。
-
@Don 好的,很高兴知道!所以问题是如何在给定函数
AWSMobileClient.default().getTokens中的令牌的情况下加载用户的其余信息我相信令牌与每个登录会话相关联正确吗?
标签: ios swift amazon-web-services graphql aws-amplify