我正在尝试使用以下方法基于来自bd或服务器的连接获取结果,但出现Expecting a element error。
我试图获取呼叫API并将数据存储在房间中,然后更新UI。
按照该方法,我曾经解决过问题,但没有运气。
* Used for get User data
*/
@UseExperimental(ObsoleteCoroutinesApi::class, ExperimentalCoroutinesApi::class)
@Throws(APIFailureException::class)
override fun getUserData(userId: String): Flow<UserData> {
return flow<UserData> {
supervisorScope {
val userCount = userDataDao.getUserCount()
val dbJob = async {
val userData: Observable<UserDataEntity> = userDataDao.getUserData()
val iterator = userData.openSubscription().iterator()
while (isActive && iterator.hasNext()) {
val next = iterator.next()?.toDomainModel()
if (next != null) {
emit(next)
}
}
}
val cm =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val apiJob = if (cm.activeNetworkInfo.isConnected) { //Check for network
async {
try {
val responseUserData =
userServices.getUserDetail(userId, true).handledResponse()?.data
if (responseUserData != null) {
saveUserData(responseUserData.toDomainModel())
responseUserData.groupRole?.let { groupRole ->
saveGroupRole(groupRole.toDomainMode())
}
saveTeamInspectionTypes(responseUserData.teamInspectionTypes.toDomainModels())
} else {
throw APIFailureException("Unable to parse Userdata")
}
} catch (e: APIFailureException) {
if (userCount <= 1) {
throw e
} else {
logger.error(e) { "Unable to update from network" }
}
}
}
} else if (userCount <= 1) {
throw NoSuchElementException("Unable to get data and no backup")
} else null
apiJob?.await() // Do not pass this line until the api job is finished (or if it is null)
dbJob.await()
}
}
}
在编译时出现以下错误。
"e: D:\ARNextGenWorkSpace\ar-mobile-next\repository\src\main\java\com\arinspect\repository\UserRepositoryImpl.kt: (73, 9): Expecting an element
e: D:\ARNextGenWorkSpace\ar-mobile-next\repository\src\main\java\com\arinspect\repository\UserRepositoryImpl.kt: (76, 17): Expecting an element
e: D:\ARNextGenWorkSpace\ar-mobile-next\repository\src\main\java\com\arinspect\repository\UserRepositoryImpl.kt: (78, 17): Expecting an element
e: D:\ARNextGenWorkSpace\ar-mobile-next\repository\src\main\java\com\arinspect\repository\UserRepositoryImpl.kt: (89, 17): Expecting an element
e: D:\ARNextGenWorkSpace\ar-mobile-next\repository\src\main\java\com\arinspect\repository\UserRepositoryImpl.kt: (117, 17): Expecting an element"