使用RXJava进行android多领域查询

时间:2017-12-11 15:14:50

标签: android kotlin realm rx-java2

我正在维护一个我自己没做过的应用程序。

该应用程序在特殊的Android设备上运行,可以使用激光束读取条形码,一旦输入条形码,应用程序开始检查领域数据库的lal,如果这样的代码存在,那么搜索查询很多,因为应用程序需要搜索所有小写字母和所有大写字母条形码。

每个数据库项目包含3个条形码和一次UPC代码,搜索方法需要迭代所有这些代码并返回答案。

以下代码

fun readItemByMKTorUPC(Value : String,callback:NewCountingPresenter.findItem{
    doAsync {
    val realm =  Realm.getDefaultInstance()
    var findVal : ItemRealmSearchOnly? = ItemRealmSearchOnly()

    //--------------------MKT-------------------------------
    //search for MKT
    findVal = realm.where(ItemRealmSearchOnly::class.java).contains("company_id",MyApplication.instance.company.companyNameId)?.equalTo("MKT",Value)?.findFirst()


    //if not found search for UPC Upper
    if(findVal?.MKT.isNullOrEmpty()){
        val Value :String = Value.toUpperCase();
        findVal = realm.where(ItemRealmSearchOnly::class.java).contains("company_id",MyApplication.instance.company.companyNameId)?.equalTo("UPC",Value)?.findFirst()
    }

    //if not found search for MKT Lower Case
    if(findVal?.MKT.isNullOrEmpty()){
        val Value :String = Value.toLowerCase();
        findVal = realm.where(ItemRealmSearchOnly::class.java).contains("company_id",MyApplication.instance.company.companyNameId)?.equalTo("MKT",Value)?.findFirst()
    }


    //if not found search for MKT UPPER Case
    if(findVal?.MKT.isNullOrEmpty()){
        val Value :String = Value.toUpperCase();
        findVal = realm.where(ItemRealmSearchOnly::class.java).contains("company_id",MyApplication.instance.company.companyNameId)?.equalTo("MKT",Value)?.findFirst()
    }

    //--------------------UPC-------------------------------
    //if not found search for UPC
    if(findVal?.MKT.isNullOrEmpty()){
        val Value :String = Value
        findVal = realm.where(ItemRealmSearchOnly::class.java).contains("company_id",MyApplication.instance.company.companyNameId)?.equalTo("UPC",Value)?.findFirst()
    }

    //if not found search for UPC Lower
    if(findVal?.MKT.isNullOrEmpty()){
        val Value :String = Value.toLowerCase();
        findVal = realm.where(ItemRealmSearchOnly::class.java).contains("company_id",MyApplication.instance.company.companyNameId)?.equalTo("UPC",Value)?.findFirst()
    }


   //--------------------MKT 2-------------------------------
    //if not found search for MKT 2
    if(findVal?.MKT.isNullOrEmpty()){
        val Value :String = Value;
        findVal = realm.where(ItemRealmSearchOnly::class.java).contains("company_id",MyApplication.instance.company.companyNameId)?.equalTo("MKT2",Value)?.findFirst()
    }
    //if not found search for MKT 2 Upper
    if(findVal?.MKT.isNullOrEmpty()){
        val Value :String = Value.toUpperCase();
        findVal = realm.where(ItemRealmSearchOnly::class.java).contains("company_id",MyApplication.instance.company.companyNameId)?.equalTo("MKT2",Value)?.findFirst()
    }
    //if not found search for MKT 2 lower
    if(findVal?.MKT.isNullOrEmpty()){
        val Value :String = Value.toLowerCase();
        findVal = realm.where(ItemRealmSearchOnly::class.java).contains("company_id",MyApplication.instance.company.companyNameId)?.equalTo("MKT2",Value)?.findFirst()
    }
    //--------------------MKT3-------------------------------
    //if not found search for MKT 3
    if(findVal?.MKT.isNullOrEmpty()){
        val Value :String = Value.toUpperCase();
        findVal = realm.where(ItemRealmSearchOnly::class.java).contains("company_id",MyApplication.instance.company.companyNameId)?.equalTo("MKT3",Value)?.findFirst()
    }

    //if not found search for MKT 3 Upper
    if(findVal?.MKT.isNullOrEmpty()){
        val Value :String = Value.toUpperCase();
        findVal = realm.where(ItemRealmSearchOnly::class.java).contains("company_id",MyApplication.instance.company.companyNameId)?.equalTo("MKT3",Value)?.findFirst()
    }


    //if not found search for MKT 3 lower
    if(findVal?.MKT.isNullOrEmpty()){
        val Value :String = Value.toUpperCase();
        findVal = realm.where(ItemRealmSearchOnly::class.java).contains("company_id",MyApplication.instance.company.companyNameId)?.equalTo("MKT3",Value)?.findFirst()
    }

    var item : ItemRealm = ItemRealm();
    item.MKT = findVal?.MKT;
    item.MKT2 = findVal?.MKT2;
    item.MKT3 = findVal?.MKT3;
    item.UPC = findVal?.UPC;
    item.AMOUNT = findVal?.AMOUNT;
    uiThread {

            callback.clickHere(item);
        }

    }
}

此方法的问题是,需要很长时间来迭代每个查询并向用户返回答案

我想为此代码集成RxJava 2方法,但无法找到有关如何在线进行此操作的有用提示。

编辑:

我找到了一个解决方案,感谢 Tim Castelijns 在评论中暗示

        MyApplication.instance.printLog("scanner start: MKT");
        findVal = realm.where(ItemRealmSearchOnly::class.java)
                .contains("company_id",MyApplication.instance.company.companyNameId)
                ?.equalTo("MKT",Value)
                ?.or()?.equalTo("MKT",Value.toUpperCase())
                ?.or()?.equalTo("MKT",Value.toLowerCase())
                ?.or()?.equalTo("UPC",Value)
                ?.or()?.equalTo("UPC",Value.toLowerCase())
                ?.or()?.equalTo("UPC",Value.toUpperCase())
                ?.or()?.equalTo("MKT2",Value)
                ?.or()?.equalTo("MKT2",Value.toLowerCase())
                ?.or()?.equalTo("MKT2",Value.toUpperCase())
                ?.or()?.equalTo("MKT3",Value)
                ?.or()?.equalTo("MKT3",Value.toLowerCase())
                ?.or()?.equalTo("MKT3",Value.toUpperCase())
                ?.findFirst();
        MyApplication.instance.printLog("scanner end: MKT");

将搜索时间比旧方法提高了至少200%

0 个答案:

没有答案