为 AWS AppSync Client iOS Swift 启用缓存



我正在使用AWS AppSync来创建我的iOS应用程序。我想利用离线突变以及AppSync提供的查询缓存。但是当我关闭互联网时,我没有得到任何回应。而是显示错误为"互联网连接似乎处于脱机状态"。这似乎是一个Alamofire例外,而不是一个AppSync例外。这是因为查询未缓存在我的设备中。以下是我初始化客户端的代码片段。

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: AWSCognitoUserPoolsAuthProvidergetLatestAuthToken()方法来获取最新的令牌。这是返回存储在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应用程序正确实现缓存,以便我也可以在没有互联网的情况下进行查询吗?

好的,所以我自己找到了答案。数据库 URL 是一个可选参数。当我们初始化 AWSAppSyncClientConfiguration 对象时,它不会出现在建议中。

因此,我初始化客户端的新方法如下

let databaseURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(AWSConstants.DATABASE_NAME, isDirectory: false)
do {
let appSyncClientConfig = try AWSAppSyncClientConfiguration.init(url: AWSConstants.APP_SYNC_ENDPOINT,
serviceRegion: AWSConstants.AWS_REGION,
userPoolsAuthProvider: MyCognitoUserPoolsAuthProvider(),
urlSessionConfiguration: URLSessionConfiguration.default,
databaseURL: databaseURL)
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)")
}

希望对您有所帮助。

最新更新