53967

How to return a Rx Single transaction using Room Db?

<h3>Question</h3>

Suppose there is a Dao class with the following two methods:

1)

delete(items: List<Item>): Completable

2)

insert(items: List< Item >): Single<List<Long>>

How can I chain them into a @transaction method in Dao class starting with ‘delete method’ and then returning ‘insert method’ result?

I want to have a method with a signature like this:

@Transaction fun deleteAndInsert(): Single<List<Long> > { ... }
<h3>Answer1:</h3>

I'm assuming your main goal is to have the return type of deleteAndInsert() as Single.

You can achieve that with small modifications

<ul><li>first by making delete() and insert() functions synchronous.</li> <li>Since @Transaction only works synchronously, we need to create another function that calls both delete() and insert(). Also, annotate this function with @Transaction</li> <li>Create another new function that creates a Single and calls the above function.</li> </ul><hr />abstract class SampleDao{ protected abstract fun delete() protected abstract fun insert(items: List<Item>) : List<Long> @Transaction protected open fun deleteAndInsertSync(items: List<Item>): List<Long>{ delete() return insert(items) } fun deleteAndInsert(items:List<Item>): Single<List<Long>>{ return Single.create { it.onSuccess(deleteAndInsertSync(items)) } } }
<h3>Answer2:</h3>

I don't think it is possible.

I once tried it and I got a compile-time error saying:

<blockquote>

"Method annotated with @Transaction must not return deferred/async return type io.reactivex.Single. Since transactions are thread confined and Room cannot guarantee that all queries in the method implementation are performed on the same thread, only synchronous @Transaction implemented methods are allowed. If a transaction is started and a change of thread is done and waited upon then a database deadlock can occur if the additional thread attempts to perform a query. This restriction prevents such situation from occurring."

</blockquote>
<h3>Answer3:</h3>

If you delete and insert methods are marked with annotation, they are executing in a transaction. So, basicly you don't need to mark your deleteAndInsert method with this annotation. So, than:

fun deleteAndInsert(items: List< Item >): Single<List<Long>> { return delete().andThan(insert(items)) }
<h3>Answer4:</h3>
<ul><li>Method annotated with @Transaction must not return deferred/async return type io.reactivex.Single. Since transactions are thread confined and Room cannot guarantee that all queries in the method implementation are performed on the same thread, only synchronous @Transaction implemented methods are allowed. If a transaction is started and a change of thread is done and waited upon then a database deadlock can occur if the additional thread attempts to perform a query. This restrictions prevents such situation from occurring.</li> <li>

When I decompile code to java, I see.

public interface CustomerDao { @Transaction @NotNull List deleteAndCreate(@NotNull List var1); @Query("DELETE FROM customer") @NotNull Completable deleteAll(); @Insert @NotNull List insertAll(@NotNull List var1); @Metadata( mv = {1, 1, 15}, bv = {1, 0, 3}, k = 3 ) public static final class DefaultImpls { @Transaction @NotNull public static List deleteAndCreate(CustomerDao $this, @NotNull List users) { Intrinsics.checkParameterIsNotNull(users, "users"); $this.deleteAll(); return $this.insertAll(users); } } } </li> </ul>
<h3>Answer5:</h3>

Get Ids of new Data

then delete all items where not in the new data

@Query("DELETE FROM product WHERE id NOT IN(:idsNewItems)") @Override public abstract void deleteOldItems(List<String> idsNewItems)

;

来源:https://stackoverflow.com/questions/55463342/how-to-return-a-rx-single-transaction-using-room-db

Recommend

  • How to return a Rx Single transaction using Room Db?
  • how to combine two grabbed variables using preg_match_all?
  • Typescript compiler error - supplied parameters do not match any signature of call target
  • Dynamic form for submit values
  • After updating xdan/datetimepicker from 2.4.0 to 2.5.4 - $.datetimepicker is undefined
  • how to redirect python's subprocess.Call methods output to a file and not console?
  • Cannot insert relationship ID into Aldeed's Autoform MeteorJS framework
  • How to copy file to local directory using Ansible?
  • Flash Pro AS 3.0 Air for iOS, Orientation Event not working
  • SherlockActivity cannot be resolved to a type in android
  • AWS EC2 tomcat permission denied creating/writing to file
  • How to place UI widgets on top of multiple Z ordered Surface Views in Android
  • What method is called before the screen goes black after inactivity? [duplicate]
  • Get Index of Item Text in MFC CListCtrl
  • Caching of Google Cloud Endpoints?
  • PhoneGap FileReader/readAsDataURL Not Triggering Callbacks
  • How do I handle a WebSocket close from the client in Yaws?
  • java: reading large file with charset
  • How to clone select and range inputs in jQuery mobile 1.3.2
  • GAE python NDB projection query working in development but not in production
  • How can I progmatically click a link in a Winform Web Browser control?
  • Javascript visualization package for multidimentional bar chart
  • Appending spaces with str_pad
  • Problems installing Common Lisp Sketch into Windows 10
  • Having a custom repository with DBAL connection in Symfony 2 / Doctrine 2?
  • Tortoise SVN conflict occurs but no edit conflict button is shown
  • Using WCF service via async interface from worker thread, how do I ensure that events are sent from
  • Intersection of characters in two strings
  • Alternative to bcrypt when saving passwords in PHP 5.2
  • How to enable LDAP extension in XAMPP environment
  • Map Annotation Disclosure Indicator - Xamarin.Form
  • Selenium Webdriver IE could not find element
  • Why am I getting an Argument exception when creating event handler dynamically?
  • Populating a database table with returned JSON
  • Protractor Page objects - TypeError: Object # has no method 'methodName'
  • how to add dashed border on highcharts “area” graph for every point
  • How to use Typescript with libraries like Ampersand.js that parse configs to build prototypes
  • Background transfer download task failed when app was closed
  • ssh remote server login script