Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第一次走进 Android 中的 Kotlin 协程 #1808

Merged
merged 5 commits into from
Jul 5, 2017
Merged

Conversation

Feximin
Copy link
Contributor

@Feximin Feximin commented Jun 26, 2017

@wilsonandusa
Copy link
Contributor

校对认领 @sqrthree

@linhe0x0
Copy link
Member

@wilsonandusa 好的呢 🍺

@@ -138,11 +139,11 @@ This is the resulting code:
bindForecast(result.await())
}

This is nice! The forecast is requested in a background thread thanks to the `bg` function, which will return a deferred result. That result is awaited in the `bindForecast` call, until it’s ready to be returned.
太棒了!天气预报数据是在一个后台线程中请求的,这多亏了 `bg` 方法,这个方法返回了一个延迟结果。那个延迟结果一直在 `bindForecast` 调用中等待,直到真正返回。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[那个延迟结果一直在 bindForecast 调用中等待直到真正返回。] -> [延迟的结果在可以返回前会一直在bindForecast 调用中等待]

Copy link
Contributor

@wilsonandusa wilsonandusa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sqrthree @Feximin 校对完成 翻译的很赞

@phxnirvana
Copy link
Contributor

周六没人领就我领(逃

@atuooo
Copy link
Contributor

atuooo commented Jun 30, 2017

@sqrthree 校对认领。刚跑错地方了😅

@linhe0x0
Copy link
Member

@atuooo 妥妥哒 🍻

@linhe0x0
Copy link
Member

linhe0x0 commented Jul 3, 2017

@atuooo 不要忘了来校对啊

Copy link
Contributor

@atuooo atuooo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Feximin @sqrthree 校对完啦


If you were a former C# developer, async/await is the closest concept. But coroutines in Kotlin are more powerful, because instead of being a specific implementation of the idea, they are **a language feature that can be implemented in different ways to solve different problems**.
简单来说,协程是一种按序写异步代码的方式。**你可以一行一行的写代码,而不是到处都有乱七八糟的回调**。有的还将会有暂停执行然后等待结果返回的能力。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一行一行地


My goal with this article is that you are able to get some basic concepts and use one of the existing libraries, not to build your own implementations. But I think it’s important to understand some of the internals so that you don’t just blindly use what you are given.
本文的目的旨在让你了解一些基本概念,会用一个现有的库,而不是去自己去实现一个。但我认为重要的是了解一些内部原理,这样你就不会盲目使用了。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本文的目的旨在

“旨在“ 是有达到某种目的的意思,重复了, 可以译成”本文的目的是让你。。“


compile “org.jetbrains.anko:anko-coroutines:$anko_version”

Next, if you remember, I told you that you need to opt in for the feature, otherwise it will show a warning. To do that, simply add this line to the `gradle.properties` file in root folder (create it if it doesn’t exist yet):
接下来,如果你还记得,我曾经告诉过你需要有所选择地使用这个功能,否则就会有警告。要做到这一点,只需要简单地在根文件夹下的 `gradle.properties` 文件(如果不存在就创建)中添加这一行:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我曾经告诉过你需要有所选择地使用这个功能,否则就会有警告

我曾告诉过你需要选择是否开启这个功能,否则会出现警告

要做到这一点

要解决这个问题


But not everything is great. What’s happening here? Coroutines have a problem: **they are keeping a reference to `DetailActivity`, leaking it if the request never finishes** for instance.
但并不是一切都好。发生了什么?协程遇到一个问题:例如:**他们持有一个 `DetailActivity` 的引用,如果这个请求永不结束就会内存泄露**。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该不需要加 “例如”

@@ -152,11 +153,11 @@ Don’t worry, because Anko has a solution. You can create a weak reference to y
ref().bindForecast(result.await())
}

This reference will allow calling the activity when it’s available, and will cancel the coroutine in case the activity has been killed. Be careful to ensure that all calls to activity methods or properties are done via this `ref` object.
在 activity 可用时,弱引用允许访问 activity,假使 activity 被杀死就取消协程。需要仔细确保的是所有对 activity 中的方法或属性的调用都要经过这个 `ref` 对象。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

假使 activity 被杀死就取消协程

当 activity 被杀死,协程将会取消

@linhe0x0
Copy link
Member

linhe0x0 commented Jul 4, 2017

@Feximin 两位校对者都已经校对好了~ 可以来根据校对意见进行调整了哈 ┏ (゜ω゜)=☞

@Feximin
Copy link
Contributor Author

Feximin commented Jul 5, 2017

@wilsonandusa @atuooo @sqrthree 调整完啦,感谢两位校对大佬~

Copy link
Member

@linhe0x0 linhe0x0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文中有几处用了斜体了,请参考 wiki 中斜体文字使用加粗样式代替 的要求哈。

@@ -1,48 +1,49 @@
> * 原文地址:[A first walk into Kotlin coroutines on Android](https://android.jlelse.eu/a-first-walk-into-kotlin-coroutines-on-android-fe4a6e25f46a)
> * 原文作者:[Antonio Leiva](https://android.jlelse.eu/@antoniolg)
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner)
> * 译者:
> * 译者:[Feximin](https://github.com/Feximin)
> * 校对者:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

校对者信息记得填一下哈

@Feximin
Copy link
Contributor Author

Feximin commented Jul 5, 2017

@sqrthree 激动了哈,请帮忙看下还有没有要改的。。

@linhe0x0 linhe0x0 merged commit 8e199f9 into xitu:master Jul 5, 2017
@linhe0x0
Copy link
Member

linhe0x0 commented Jul 5, 2017

@Feximin 已经 merge 啦~ 快快麻溜发布到掘金专栏然后给我发下链接,方便及时添加积分哟。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants