|
26 | 26 | import android.database.sqlite.SQLiteOpenHelper;
|
27 | 27 | import android.database.sqlite.SQLiteProgram;
|
28 | 28 | import android.database.sqlite.SQLiteStatement;
|
| 29 | +import android.database.sqlite.SQLiteTransactionListener; |
29 | 30 | import android.support.annotation.VisibleForTesting;
|
30 | 31 | import com.google.common.base.Function;
|
31 | 32 | import com.google.firebase.firestore.auth.User;
|
@@ -75,6 +76,21 @@ public static String databaseName(String persistenceKey, DatabaseId databaseId)
|
75 | 76 | private final SQLiteQueryCache queryCache;
|
76 | 77 | private final SQLiteRemoteDocumentCache remoteDocumentCache;
|
77 | 78 | private final SQLiteLruReferenceDelegate referenceDelegate;
|
| 79 | + private final SQLiteTransactionListener transactionListener = |
| 80 | + new SQLiteTransactionListener() { |
| 81 | + @Override |
| 82 | + public void onBegin() { |
| 83 | + referenceDelegate.onTransactionStarted(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void onCommit() { |
| 88 | + referenceDelegate.onTransactionCommitted(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void onRollback() {} |
| 93 | + }; |
78 | 94 |
|
79 | 95 | public SQLitePersistence(
|
80 | 96 | Context context, String persistenceKey, DatabaseId databaseId, LocalSerializer serializer) {
|
@@ -143,35 +159,32 @@ RemoteDocumentCache getRemoteDocumentCache() {
|
143 | 159 |
|
144 | 160 | @Override
|
145 | 161 | void runTransaction(String action, Runnable operation) {
|
| 162 | + Logger.debug(TAG, "Starting transaction: %s", action); |
| 163 | + db.beginTransactionWithListener(transactionListener); |
146 | 164 | try {
|
147 |
| - Logger.debug(TAG, "Starting transaction: %s", action); |
148 |
| - referenceDelegate.onTransactionStarted(); |
149 |
| - db.beginTransaction(); |
150 | 165 | operation.run();
|
151 | 166 |
|
152 | 167 | // Note that an exception in operation.run() will prevent this code from running.
|
153 | 168 | db.setTransactionSuccessful();
|
154 | 169 | } finally {
|
155 | 170 | db.endTransaction();
|
156 |
| - referenceDelegate.onTransactionCommitted(); |
157 | 171 | }
|
158 | 172 | }
|
159 | 173 |
|
160 | 174 | @Override
|
161 | 175 | <T> T runTransaction(String action, Supplier<T> operation) {
|
| 176 | + Logger.debug(TAG, "Starting transaction: %s", action); |
| 177 | + T value = null; |
| 178 | + db.beginTransactionWithListener(transactionListener); |
162 | 179 | try {
|
163 |
| - Logger.debug(TAG, "Starting transaction: %s", action); |
164 |
| - referenceDelegate.onTransactionStarted(); |
165 |
| - db.beginTransaction(); |
166 |
| - T value = operation.get(); |
| 180 | + value = operation.get(); |
167 | 181 |
|
168 | 182 | // Note that an exception in operation.run() will prevent this code from running.
|
169 | 183 | db.setTransactionSuccessful();
|
170 |
| - return value; |
171 | 184 | } finally {
|
172 | 185 | db.endTransaction();
|
173 |
| - referenceDelegate.onTransactionCommitted(); |
174 | 186 | }
|
| 187 | + return value; |
175 | 188 | }
|
176 | 189 |
|
177 | 190 | /**
|
|
0 commit comments