【发布时间】:2019-02-21 19:46:27
【问题描述】:
我正在使用AWS AppSync 创建我的 iOS 应用程序。我想利用AppSync 提供的离线突变和查询缓存。但是当我关闭我的互联网时,我没有得到任何回应。而是将错误显示为“Internet 连接似乎已脱机。”。这似乎是Alamofire 异常而不是AppSync 异常。这是因为查询没有缓存在我的设备中。以下是我的代码 sn-p 来初始化客户端。
do {
let appSyncClientConfig = try AWSAppSyncClientConfiguration.init(url: AWSConstants.APP_SYNC_ENDPOINT, serviceRegion: AWSConstants.AWS_REGION, userPoolsAuthProvider: MyCognitoUserPoolsAuthProvider())
AppSyncHelper.shared.appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncClientConfig)
AppSyncHelper.shared.appSyncClient?.apolloClient?.cacheKeyForObject = { $0["id"] }
} catch {
print("Error in initializing the AppSync Client")
print("Error: \(error)")
UserDefaults.standard.set(nil, forKey: DeviceConstants.ID_TOKEN)
}
我在获取会话时将令牌缓存在UserDefaults 中,然后每当调用AppSyncClient 时,它都会通过调用我的MyCognitoUserPoolsAuthProvider: AWSCognitoUserPoolsAuthProvider 的getLatestAuthToken() 方法来获取最新的令牌。这将返回存储在UserDefaults 中的令牌 -
// background thread - asynchronous
func getLatestAuthToken() -> String {
print("Inside getLatestAuthToken")
var token: String? = nil
if let tokenString = UserDefaults.standard.string(forKey: DeviceConstants.ID_TOKEN) {
token = tokenString
return token!
}
return token!
}
我的查询模式如下
public func getUserProfile(userId: String, success: @escaping (ProfileModel) -> Void, failure: @escaping (NSError) -> Void) {
let getQuery = GetUserProfileQuery(id: userId)
print("getQuery.id: \(getQuery.id)")
if appSyncClient != nil {
print("AppSyncClient is not nil")
appSyncClient?.fetch(query: getQuery, cachePolicy: CachePolicy.returnCacheDataElseFetch, queue: DispatchQueue.global(qos: .background), resultHandler: { (result, error) in
if error != nil {
failure(error! as NSError)
} else {
var profileModel = ProfileModel()
print("result: \(result)")
if let data = result?.data {
print("data: \(data)")
if let userProfile = data.snapshot["getUserProfile"] as? [String: Any?] {
profileModel = ProfileModel(id: UserDefaults.standard.string(forKey: DeviceConstants.USER_ID), username: userProfile["username"] as? String, mobileNumber: userProfile["mobileNumber"] as? String, name: userProfile["name"] as? String, gender: (userProfile["gender"] as? Gender).map { $0.rawValue }, dob: userProfile["dob"] as? String, profilePicUrl: userProfile["profilePicUrl"] as? String)
} else {
print("data snapshot is nil")
}
}
success(profileModel)
}
})
} else {
APPUtilites.displayErrorSnackbar(message: "Error in the user session. Please login again")
}
}
AppSync提供的4个CachePolicy对象我都用过了,即
CachePolicy.returnCacheDataElseFetch
CachePolicy.fetchIgnoringCacheData
CachePolicy.returnCacheDataDontFetch
CachePolicy.returnCacheDataAndFetch.
有人可以帮助我为我的 iOS 应用正确实现缓存,以便我也可以在没有互联网的情况下进行查询吗?
【问题讨论】:
标签: ios swift caching aws-appsync aws-appsync-ios