握持网络呼叫后提供依赖性
我需要为店面API提供图形客户端,但是在进行网络调用后,我只能构建客户端。
@Provides
@Singleton
fun getGraphClient(context: Context, client: Client): GraphClient {
return GraphClient.build(
context = context,
shopDomain = client.shopifyDomain,
accessToken = client.storefrontAccessToken
)
}
在进行网络调用以使客户端将其传递给刀片以返回图表客户端后,该如何?
I need to provide a graph client for Storefront API but I can only build the client after making a network call.
@Provides
@Singleton
fun getGraphClient(context: Context, client: Client): GraphClient {
return GraphClient.build(
context = context,
shopDomain = client.shopifyDomain,
accessToken = client.storefrontAccessToken
)
}
How can I after making a network call to get the client pass it to hilt to return the graph client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过这样的手动依赖注入解决了此问题:
然后在API调用后将
其注入到任何此类存储库中:
I solved this by manual dependency injection like this:
And then after the API call
And inject into any repository like this:
您的解决方案根本不是剑术方法。您可以在没有@Module接口的情况下执行此操作。您应该像以前一样提供图形点,并注入以下内容:
如果您不熟悉Kotlin“ LateInit”,那么您会发现参考文献直到第一次使用某个地方才能实例化。
我添加了LateInit关键字,并修改了一些您提到的代码段。
请让我知道这是否有效,如果您不觉得它有帮助 - 请在评论中告诉我。我一定会唯一您的刀柄问题。
因为您的帖子年纪大了,您可能已经知道一种更好的方法。但是我只是不想让任何人感到困惑,因为您的解决方案不是实际的剑术方式。因此,我认为我应该指导您获得专家在生产代码方面的解决方案。
愉快的编码!
Your solution is not Hilt approach at all. You could do it without @Module interface. You should provide the GraphClient like you did before and inject like following:
if you are not familiar with kotlin "lateinit" then you will find out the reference does not instantiate until it's used somewhere for the first time.
I added lateinit keyword and modified a little of your mentioned code snippet.
Please let me know if this works, If you don't find it helpful - just let me know in comments. I will definitely sole your hilt issues.
As your post is much older and you may already know a better approach. But I just don't want anyone confused others that your solution is not actual Hilt way. So I thought i should guide you to the solution an expert do in production codes.
Happy coding!