Android我的代码rxjava缓慢工作
问题描述:
在我的应用程序中有一种通过API获取数据的方法。它在IntentService中执行。接收到的数据被写入数据库。我在rxjava的帮助下重写了这个方法,但执行时间大大增加了。 这是我在IntentService代码Android我的代码rxjava缓慢工作
Call<List<DocLine>> DocLineResult = mApi.getDocLine(UserID, LastUserDate);
Response<List<DocLine>> responseDocLine = DocLineResult.execute();
if (responseDocLine.isSuccessful()) {
List<DocLine> docLines = responseDocLine.body();
if (docLines != null) {
for (DocLine item : docLines) {
mContents = new ContentValues();
//filling mContents from item
dbBase.insertWithOnConflict(dbTabName, null, mContents, SQLiteDatabase.CONFLICT_REPLACE);
}
Log.d(AppGlobal.LOG_TAG, dbTabName + " => " + docLines.size());
}
}
else {
if (responseDocHead.raw() != null && responseDocHead.raw().code() == 404) {
throw new Exception(getString(R.string.error_null_user));
else
throw new Exception("Error");
}
}
这里是我的代码rxjava
Observable oDocLine = apiService.getDocLine(userId, lastSyncDate)
.flatMap(Observable::from)
.flatMap(docLine -> Observable.create(subscriber -> {workDB.save(docLine, null); subscriber.onCompleted();})
.subscribeOn(Schedulers.io()));
答
你第二flatmap似乎起不到任何有用的目的除了增加一个副作用。
Observable oDocLine = apiService.getDocLine(userId, lastSyncDate)
.flatMap(Observable::from)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(docLine -> workDB.save(docLine, null));
应该是希望更有效率。
请不要混合使用pascalcase和camelcase进行变量命名。它使你的代码无法阅读 –
谢谢你的批评 – user1854307
它的建设性,尝试遵循java命名约定 –