尝试从android中的列表中删除某些项目时出现ConcurrentModificationException

时间:2019-05-25 19:38:34

标签: android

我有过滤器品牌的方法:

class GetBrandsUseCase @Inject constructor(private val getProductsRepository: ProductsRepository,
                                           private val getMenuBrandsUseCase: GetMenuBrandsUseCase) : SingleNoParamUseCase<List<String>> {
    override fun run(): Single<List<String>> {
        val listOfBrand = mutableListOf<String>()
        return getProductsRepository.getProductsFromMemory()
                .doOnError {  }
                .map {
                    it.map { productModel ->
                        if (!listOfBrand.contains(productModel.attributes.brand) && productModel.attributes.brand.isNotBlank())
                            listOfBrand.add(productModel.attributes.brand)
                    }
                    listOfBrand
                }.flatMap {
                    getMenuBrandsUseCase.run()
                }.map { list ->
                    if (list.isNotEmpty())
                        listOfBrand.removeIf {
                            !list.contains(it)
                        }
                    listOfBrand
                }
    }
}

此方法有效,但有时会出错:

FATAL EXCEPTION: RxCachedThreadScheduler-4
Process: de.greeneye.eva.debug, PID: 7379
io.reactivex.exceptions.OnErrorNotImplementedException: The exception was not handled due to missing onError handler in the subscribe() method call. Further reading: https://github.com/ReactiveX/RxJava/wiki/Error-Handling | java.util.ConcurrentModificationException
    at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
    at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
    at io.reactivex.internal.observers.ConsumerSingleObserver.onError(ConsumerSingleObserver.java:46)
    at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.onError(SingleSubscribeOn.java:73)
    at io.reactivex.internal.operators.single.SingleMap$MapSingleObserver.onError(SingleMap.java:69)
    at io.reactivex.internal.operators.single.SingleFlatMap$SingleFlatMapCallback.onError(SingleFlatMap.java:90)
    at io.reactivex.internal.operators.single.SingleMap$MapSingleObserver.onError(SingleMap.java:69)
    at io.reactivex.internal.operators.single.SingleMap$MapSingleObserver.onSuccess(SingleMap.java:60)
    at io.reactivex.internal.operators.single.SingleDoOnError$DoOnError.onSuccess(SingleDoOnError.java:52)
    at io.reactivex.internal.operators.single.SingleJust.subscribeActual(SingleJust.java:30)
    at io.reactivex.Single.subscribe(Single.java:3495)
    at io.reactivex.internal.operators.single.SingleDoOnError.subscribeActual(SingleDoOnError.java:35)
    at io.reactivex.Single.subscribe(Single.java:3495)
    at io.reactivex.internal.operators.single.SingleMap.subscribeActual(SingleMap.java:34)
    at io.reactivex.Single.subscribe(Single.java:3495)
    at io.reactivex.internal.operators.single.SingleFlatMap.subscribeActual(SingleFlatMap.java:36)
    at io.reactivex.Single.subscribe(Single.java:3495)
    at io.reactivex.internal.operators.single.SingleMap.subscribeActual(SingleMap.java:34)
    at io.reactivex.Single.subscribe(Single.java:3495)
    at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
    at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:762)
 Caused by: java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.next(ArrayList.java:831)
    at de.greeneye.domain.filters.GetBrandsUseCase$run$2.apply(GetBrandsUseCase.kt:33)
    at de.greeneye.domain.filters.GetBrandsUseCase$run$2.apply(GetBrandsUseCase.kt:9)

有什么想法可以解决此问题吗?

0 个答案:

没有答案