Created
January 12, 2018 14:23
-
-
Save mkoslacz/ec39fefe5956a8fd1e9636ee0810d5d8 to your computer and use it in GitHub Desktop.
Sample malfunctioning transaction usecase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T : Any> Observable<List<T>>.replaceInLocalRepo(localRepository: Repository<T>, | |
localSpecification: Specification): Observable<List<T>> = | |
this.flatMapSingle { incomingEntities -> | |
runInTransactionSingle { | |
incomingEntities | |
.asSingle() | |
.flatMap { | |
localRepository | |
.remove(localSpecification) | |
.toSingleDefault(incomingEntities) | |
.onErrorReturn { | |
it.printAndReport() | |
incomingEntities | |
} | |
} | |
.flatMap { | |
localRepository | |
.add(incomingEntities) | |
.toSingleDefault(incomingEntities) | |
.onErrorReturn { | |
it.printAndReport() | |
incomingEntities | |
} | |
} | |
} | |
} | |
private fun <T : Any> runInTransactionSingle(transactionBody: ((DatabaseWrapper) -> Single<T>)) = | |
Single.create<T> { emitter -> | |
FlowManager.getDatabase(SampleDatabase::class.java) | |
.executeTransaction { | |
transactionBody(it) | |
.subscribeBy( | |
onSuccess = { emitter.onSuccess(it) }, | |
onError = { emitter.onError(it) }) | |
} | |
}.subscribeOn(Schedulers.computation())!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment