Skip to content

Commit cdc4f89

Browse files
authored
Database bug fix (#5902)
* make database function calls suspending and update room version * replace MainScope with coroutineScope for database operations * add suspend keyword and refactor code
1 parent bc065c8 commit cdc4f89

File tree

7 files changed

+28
-34
lines changed

7 files changed

+28
-34
lines changed

app/src/main/java/fr/free/nrw/commons/customselector/database/NotForUploadStatusDao.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ abstract class NotForUploadStatusDao {
1515
* Insert into Not For Upload status.
1616
*/
1717
@Insert(onConflict = OnConflictStrategy.REPLACE)
18-
abstract fun insert(notForUploadStatus: NotForUploadStatus)
18+
abstract suspend fun insert(notForUploadStatus: NotForUploadStatus)
1919

2020
/**
2121
* Delete Not For Upload status entry.
2222
*/
2323
@Delete
24-
abstract fun delete(notForUploadStatus: NotForUploadStatus)
24+
abstract suspend fun delete(notForUploadStatus: NotForUploadStatus)
2525

2626
/**
2727
* Query Not For Upload status with image sha1.
2828
*/
2929
@Query("SELECT * FROM images_not_for_upload_table WHERE imageSHA1 = (:imageSHA1) ")
30-
abstract fun getFromImageSHA1(imageSHA1: String): NotForUploadStatus?
30+
abstract suspend fun getFromImageSHA1(imageSHA1: String): NotForUploadStatus?
3131

3232
/**
3333
* Asynchronous image sha1 query.
@@ -38,7 +38,7 @@ abstract class NotForUploadStatusDao {
3838
* Deletion Not For Upload status with image sha1.
3939
*/
4040
@Query("DELETE FROM images_not_for_upload_table WHERE imageSHA1 = (:imageSHA1) ")
41-
abstract fun deleteWithImageSHA1(imageSHA1: String)
41+
abstract suspend fun deleteWithImageSHA1(imageSHA1: String)
4242

4343
/**
4444
* Asynchronous image sha1 deletion.
@@ -49,5 +49,5 @@ abstract class NotForUploadStatusDao {
4949
* Check whether the imageSHA1 is present in database
5050
*/
5151
@Query("SELECT COUNT() FROM images_not_for_upload_table WHERE imageSHA1 = (:imageSHA1) ")
52-
abstract fun find(imageSHA1: String): Int
52+
abstract suspend fun find(imageSHA1: String): Int
5353
}

app/src/main/java/fr/free/nrw/commons/customselector/database/UploadedStatusDao.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,31 @@ abstract class UploadedStatusDao {
1717
* Insert into uploaded status.
1818
*/
1919
@Insert(onConflict = OnConflictStrategy.REPLACE)
20-
abstract fun insert(uploadedStatus: UploadedStatus)
20+
abstract suspend fun insert(uploadedStatus: UploadedStatus)
2121

2222
/**
2323
* Update uploaded status entry.
2424
*/
2525
@Update
26-
abstract fun update(uploadedStatus: UploadedStatus)
26+
abstract suspend fun update(uploadedStatus: UploadedStatus)
2727

2828
/**
2929
* Delete uploaded status entry.
3030
*/
3131
@Delete
32-
abstract fun delete(uploadedStatus: UploadedStatus)
32+
abstract suspend fun delete(uploadedStatus: UploadedStatus)
3333

3434
/**
3535
* Query uploaded status with image sha1.
3636
*/
3737
@Query("SELECT * FROM uploaded_table WHERE imageSHA1 = (:imageSHA1) ")
38-
abstract fun getFromImageSHA1(imageSHA1: String): UploadedStatus?
38+
abstract suspend fun getFromImageSHA1(imageSHA1: String): UploadedStatus?
3939

4040
/**
4141
* Query uploaded status with modified image sha1.
4242
*/
4343
@Query("SELECT * FROM uploaded_table WHERE modifiedImageSHA1 = (:modifiedImageSHA1) ")
44-
abstract fun getFromModifiedImageSHA1(modifiedImageSHA1: String): UploadedStatus?
44+
abstract suspend fun getFromModifiedImageSHA1(modifiedImageSHA1: String): UploadedStatus?
4545

4646
/**
4747
* Asynchronous insert into uploaded status table.
@@ -55,7 +55,7 @@ abstract class UploadedStatusDao {
5555
* Check whether the imageSHA1 is present in database
5656
*/
5757
@Query("SELECT COUNT() FROM uploaded_table WHERE imageSHA1 = (:imageSHA1) AND imageResult = (:imageResult) ")
58-
abstract fun findByImageSHA1(
58+
abstract suspend fun findByImageSHA1(
5959
imageSHA1: String,
6060
imageResult: Boolean,
6161
): Int
@@ -66,7 +66,7 @@ abstract class UploadedStatusDao {
6666
@Query(
6767
"SELECT COUNT() FROM uploaded_table WHERE modifiedImageSHA1 = (:modifiedImageSHA1) AND modifiedImageResult = (:modifiedImageResult) ",
6868
)
69-
abstract fun findByModifiedImageSHA1(
69+
abstract suspend fun findByModifiedImageSHA1(
7070
modifiedImageSHA1: String,
7171
modifiedImageResult: Boolean,
7272
): Int

app/src/main/java/fr/free/nrw/commons/customselector/ui/selector/ImageLoader.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import fr.free.nrw.commons.utils.CustomSelectorUtils
1717
import fr.free.nrw.commons.utils.CustomSelectorUtils.Companion.checkWhetherFileExistsOnCommonsUsingSHA1
1818
import kotlinx.coroutines.CoroutineDispatcher
1919
import kotlinx.coroutines.CoroutineScope
20-
import kotlinx.coroutines.MainScope
20+
import kotlinx.coroutines.Dispatchers
2121
import kotlinx.coroutines.launch
2222
import java.util.Calendar
2323
import java.util.concurrent.TimeUnit
@@ -65,7 +65,7 @@ class ImageLoader
6565
/**
6666
* Coroutine Scope.
6767
*/
68-
private val scope: CoroutineScope = MainScope()
68+
private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO)
6969

7070
/**
7171
* Query image and setUp the view.

app/src/main/java/fr/free/nrw/commons/nearby/PlaceDao.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,21 @@ public abstract class PlaceDao {
3737
*/
3838
public Completable save(final Place place) {
3939
return Completable
40-
.fromAction(() -> {
41-
saveSynchronous(place);
42-
});
40+
.fromAction(() -> saveSynchronous(place));
4341
}
4442

4543
/**
4644
* Deletes all Place objects from the database.
47-
*
48-
* @return A Completable that completes once the deletion operation is done.
4945
*/
5046
@Query("DELETE FROM place")
5147
public abstract void deleteAllSynchronous();
5248

5349
/**
5450
* Deletes all Place objects from the database.
5551
*
52+
* @return A Completable that completes once the deletion operation is done.
5653
*/
5754
public Completable deleteAll() {
58-
return Completable
59-
.fromAction(() -> {
60-
deleteAllSynchronous();
61-
});
55+
return Completable.fromAction(this::deleteAllSynchronous);
6256
}
6357
}

app/src/main/java/fr/free/nrw/commons/upload/depicts/DepictsDao.kt

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ abstract class DepictsDao {
2222
private val maxItemsAllowed = 10
2323

2424
@Insert(onConflict = OnConflictStrategy.REPLACE)
25-
abstract fun insert(depictedItem: Depicts)
25+
abstract suspend fun insert(depictedItem: Depicts)
2626

2727
@Query("Select * From depicts_table order by lastUsed DESC")
28-
abstract fun getAllDepicts(): List<Depicts>
28+
abstract suspend fun getAllDepicts(): List<Depicts>
2929

3030
@Query("Select * From depicts_table order by lastUsed DESC LIMIT :n OFFSET 10")
31-
abstract fun getDepictsForDeletion(n: Int): List<Depicts>
31+
abstract suspend fun getDepictsForDeletion(n: Int): List<Depicts>
3232

3333
@Delete
34-
abstract fun delete(depicts: Depicts)
34+
abstract suspend fun delete(depicts: Depicts)
3535

3636
/**
3737
* Gets all Depicts objects from the database, ordered by lastUsed in descending order.
3838
*
39-
* @return A list of Depicts objects.
39+
* @return Deferred list of Depicts objects.
4040
*/
4141
fun depictsList(): Deferred<List<Depicts>> =
4242
CoroutineScope(Dispatchers.IO).async {
@@ -48,7 +48,7 @@ abstract class DepictsDao {
4848
*
4949
* @param depictedItem The Depicts object to insert.
5050
*/
51-
private fun insertDepict(depictedItem: Depicts) =
51+
fun insertDepict(depictedItem: Depicts) =
5252
CoroutineScope(Dispatchers.IO).launch {
5353
insert(depictedItem)
5454
}
@@ -59,7 +59,7 @@ abstract class DepictsDao {
5959
* @param n The number of depicts to delete.
6060
* @return A list of Depicts objects to delete.
6161
*/
62-
private suspend fun depictsForDeletion(n: Int): Deferred<List<Depicts>> =
62+
fun depictsForDeletion(n: Int): Deferred<List<Depicts>> =
6363
CoroutineScope(Dispatchers.IO).async {
6464
getDepictsForDeletion(n)
6565
}
@@ -69,7 +69,7 @@ abstract class DepictsDao {
6969
*
7070
* @param depicts The Depicts object to delete.
7171
*/
72-
private suspend fun deleteDepicts(depicts: Depicts) =
72+
fun deleteDepicts(depicts: Depicts) =
7373
CoroutineScope(Dispatchers.IO).launch {
7474
delete(depicts)
7575
}

app/src/main/java/fr/free/nrw/commons/upload/worker/UploadWorker.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ import fr.free.nrw.commons.upload.UploadProgressActivity
4141
import fr.free.nrw.commons.upload.UploadResult
4242
import fr.free.nrw.commons.wikidata.WikidataEditService
4343
import io.reactivex.schedulers.Schedulers
44+
import kotlinx.coroutines.CoroutineScope
4445
import kotlinx.coroutines.Dispatchers
45-
import kotlinx.coroutines.MainScope
4646
import kotlinx.coroutines.launch
4747
import kotlinx.coroutines.withContext
4848
import timber.log.Timber
@@ -534,7 +534,7 @@ class UploadWorker(
534534
contribution.contentUri?.let {
535535
val imageSha1 = contribution.imageSHA1.toString()
536536
val modifiedSha1 = fileUtilsWrapper.getSHA1(fileUtilsWrapper.getFileInputStream(contribution.localUri?.path))
537-
MainScope().launch {
537+
CoroutineScope(Dispatchers.IO).launch {
538538
uploadedStatusDao.insertUploaded(
539539
UploadedStatus(
540540
imageSha1,

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ android.enableR8.fullMode=false
2020
KOTLIN_VERSION=1.9.22
2121
LEAK_CANARY_VERSION=2.10
2222
DAGGER_VERSION=2.23
23-
ROOM_VERSION=2.5.0
23+
ROOM_VERSION=2.6.1
2424
PREFERENCE_VERSION=1.1.0
2525
CORE_KTX_VERSION=1.9.0
2626
ADAPTER_DELEGATES_VERSION=4.3.0

0 commit comments

Comments
 (0)