将 RxJava 代码正确转换为 Kotlin
Convert RxJava code to Kotlin properly
我正在学习 Kotlin 和 RxJava。我在 Java:
中有这样的代码
public class MainActivity extends AppCompatActivity {
private HashMap<String, Object> cacheToInsertToDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cacheToInsertToDb = new HashMap<>();
Observable.interval(1, TimeUnit.SECONDS)
.take(30) // up to 30 items
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
cacheToInsertToDb.put(aLong+"", aLong);
Log.d("Observable", cacheToInsertToDb.values().toString());
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
}
}, new Action() {
@Override
public void run() throws Exception {
}
});
Observable.interval(10, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
proceedVlues(cacheToInsertToDb.values());
cacheToInsertToDb.clear();
Log.d("CLEARED", cacheToInsertToDb.values().toString());
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
}
}, new Action() {
@Override
public void run() throws Exception {
}
});
}
private void proceedVlues(Collection<Object> values) {
for(Object v: values){
if(v instanceof Long){
Log.d("proceedVlues", v+"");
}
}
}
}
但是,当我在 AndroidStudio 中转换它时,IDE 中出现一堆错误。
这是 Kotlin 中的这段代码,在 Android Studio 中转换:
class MainActivity : AppCompatActivity() {
private var cacheToInsertToDb: HashMap<String, Any>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cacheToInsertToDb = HashMap()
Observable.interval(1, TimeUnit.SECONDS)
.take(30) // up to 30 items
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Consumer<Long> {
@Throws(Exception::class)
override fun accept(aLong: Long?) {
cacheToInsertToDb!!.put(aLong!!.toString() + "", aLong)
Log.d("Observable", cacheToInsertToDb!!.values.toString())
}
}, Consumer { }, Action { })
Observable.interval(10, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Consumer<Long> {
@Throws(Exception::class)
override fun accept(aLong: Long?) {
proceedVlues(cacheToInsertToDb!!.values)
cacheToInsertToDb!!.clear()
Log.d("CLEARED", cacheToInsertToDb!!.values.toString())
}
}, Consumer { }, Action { })
}
private fun proceedVlues(values: Collection<Any>) {
for (v in values) {
if (v is Long) {
Log.d("proceedVlues", v.toString() + "")
}
}
}
}
它给我错误下划线 "unresolved reference to @Throws" 和“'accept' 覆盖无”。如何将 Java 代码正确转换为 Kotlin?
您可以使用安全的 npe 和 lambda 更新您的代码。有时,从 java 转换为 kotlin 的代码需要一些修饰。
.subscribe(object : Consumer<Long> {
@Throws(Exception::class)
override fun accept(aLong: Long?) {
cacheToInsertToDb!!.put(aLong!!.toString() + "", aLong)
Log.d("Observable", cacheToInsertToDb!!.values.toString())
}
}, Consumer { }, Action { })
到
.subscribe(Consumer<Long?>{ aLong ->
cacheToInsertToDb?.put(aLong?.toString() ?: "", aLong)
Log.d("Observable", cacheToInsertToDb?.values.toString())
}, Consumer { }, Action { })
在 kotlin 中,您可以为 lambda 表达式替换只有一种方法的接口实现 (doc)。
Just like Java 8, Kotlin supports SAM conversions. This means that Kotlin function literals can be automatically converted into implementations of Java interfaces with a single non-default method, as long as the parameter types of the interface method match the parameter types of the Kotlin function.
这给你:
.subscribe(
{ item: Long? ->
},
{ t: Throwable ->
},
{//onComplete
})
此外,取决于您是否使用 RxJava2 (doc):
RxJava 2.x no longer accepts null values and the following will yield
NullPointerException
immediately or as a signal
因此,您确定在 onNext
中没有收到空值,您可以删除安全空值检查 ?
.subscribe(
{ item: Long ->
},
{ t: Throwable ->
},
{//onComplete
})
我正在学习 Kotlin 和 RxJava。我在 Java:
中有这样的代码public class MainActivity extends AppCompatActivity {
private HashMap<String, Object> cacheToInsertToDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cacheToInsertToDb = new HashMap<>();
Observable.interval(1, TimeUnit.SECONDS)
.take(30) // up to 30 items
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
cacheToInsertToDb.put(aLong+"", aLong);
Log.d("Observable", cacheToInsertToDb.values().toString());
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
}
}, new Action() {
@Override
public void run() throws Exception {
}
});
Observable.interval(10, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
proceedVlues(cacheToInsertToDb.values());
cacheToInsertToDb.clear();
Log.d("CLEARED", cacheToInsertToDb.values().toString());
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
}
}, new Action() {
@Override
public void run() throws Exception {
}
});
}
private void proceedVlues(Collection<Object> values) {
for(Object v: values){
if(v instanceof Long){
Log.d("proceedVlues", v+"");
}
}
}
}
但是,当我在 AndroidStudio 中转换它时,IDE 中出现一堆错误。
这是 Kotlin 中的这段代码,在 Android Studio 中转换:
class MainActivity : AppCompatActivity() {
private var cacheToInsertToDb: HashMap<String, Any>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cacheToInsertToDb = HashMap()
Observable.interval(1, TimeUnit.SECONDS)
.take(30) // up to 30 items
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Consumer<Long> {
@Throws(Exception::class)
override fun accept(aLong: Long?) {
cacheToInsertToDb!!.put(aLong!!.toString() + "", aLong)
Log.d("Observable", cacheToInsertToDb!!.values.toString())
}
}, Consumer { }, Action { })
Observable.interval(10, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Consumer<Long> {
@Throws(Exception::class)
override fun accept(aLong: Long?) {
proceedVlues(cacheToInsertToDb!!.values)
cacheToInsertToDb!!.clear()
Log.d("CLEARED", cacheToInsertToDb!!.values.toString())
}
}, Consumer { }, Action { })
}
private fun proceedVlues(values: Collection<Any>) {
for (v in values) {
if (v is Long) {
Log.d("proceedVlues", v.toString() + "")
}
}
}
}
它给我错误下划线 "unresolved reference to @Throws" 和“'accept' 覆盖无”。如何将 Java 代码正确转换为 Kotlin?
您可以使用安全的 npe 和 lambda 更新您的代码。有时,从 java 转换为 kotlin 的代码需要一些修饰。
.subscribe(object : Consumer<Long> {
@Throws(Exception::class)
override fun accept(aLong: Long?) {
cacheToInsertToDb!!.put(aLong!!.toString() + "", aLong)
Log.d("Observable", cacheToInsertToDb!!.values.toString())
}
}, Consumer { }, Action { })
到
.subscribe(Consumer<Long?>{ aLong ->
cacheToInsertToDb?.put(aLong?.toString() ?: "", aLong)
Log.d("Observable", cacheToInsertToDb?.values.toString())
}, Consumer { }, Action { })
在 kotlin 中,您可以为 lambda 表达式替换只有一种方法的接口实现 (doc)。
Just like Java 8, Kotlin supports SAM conversions. This means that Kotlin function literals can be automatically converted into implementations of Java interfaces with a single non-default method, as long as the parameter types of the interface method match the parameter types of the Kotlin function.
这给你:
.subscribe(
{ item: Long? ->
},
{ t: Throwable ->
},
{//onComplete
})
此外,取决于您是否使用 RxJava2 (doc):
RxJava 2.x no longer accepts null values and the following will yield
NullPointerException
immediately or as a signal
因此,您确定在 onNext
中没有收到空值,您可以删除安全空值检查 ?
.subscribe(
{ item: Long ->
},
{ t: Throwable ->
},
{//onComplete
})