Android Worker 出于某种原因没有从 inputData 中提取值
Android Worker does not extract value from inputData for some reason
这是我的调试视图的 screenshot。在变量视图中可以清楚地看到 intupData
包含对应于 KEY_ISSUE_ID
(issue.id
) 但
的键的值
inputData.getString(KEY_ISSUE_ID)
结果 null
。更奇怪的是,在其他 Workers
中,同样的代码运行良好。
是否有任何可能导致此类行为的警告?
顺便说一句。这个 worker 的不同之处在于它被束缚在 ArrayCreatingInputMerger
的其他 worker 之后。根据 doc 它应该接收这些工人的输出作为输入。但它似乎没有收到这些数据。
the output of parent OneTimeWorkRequests are passed in as inputs to the children.
根据 ArrayCreatingInputMerger
documentation:
- If this is the first time we encountered the key:
- If it's an array, put it in the output
- If it's a primitive, turn it into a size 1 array and put it in the output
所以你的 inputData
有一个大小为 1 的字符串数组,而不是字符串。
因此你想使用 getStringArray()
并获取该数组的第一个元素(如果你只关心第一个元素):
val issueId = inputData.getStringArray(KEY_ISSUE_ID).firstOrNull()
这是我的调试视图的 screenshot。在变量视图中可以清楚地看到 intupData
包含对应于 KEY_ISSUE_ID
(issue.id
) 但
inputData.getString(KEY_ISSUE_ID)
结果 null
。更奇怪的是,在其他 Workers
中,同样的代码运行良好。
是否有任何可能导致此类行为的警告?
顺便说一句。这个 worker 的不同之处在于它被束缚在 ArrayCreatingInputMerger
的其他 worker 之后。根据 doc 它应该接收这些工人的输出作为输入。但它似乎没有收到这些数据。
the output of parent OneTimeWorkRequests are passed in as inputs to the children.
根据 ArrayCreatingInputMerger
documentation:
- If this is the first time we encountered the key:
- If it's an array, put it in the output
- If it's a primitive, turn it into a size 1 array and put it in the output
所以你的 inputData
有一个大小为 1 的字符串数组,而不是字符串。
因此你想使用 getStringArray()
并获取该数组的第一个元素(如果你只关心第一个元素):
val issueId = inputData.getStringArray(KEY_ISSUE_ID).firstOrNull()