Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix upload stopping because of closed realm instance #1342

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions app/src/main/java/com/infomaniak/drive/data/models/UploadFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,8 @@ open class UploadFile(
return customRealm?.let(block) ?: getRealmInstance().use(block)
}

fun getAllPendingUploadsWithoutPriority(customRealm: Realm? = null): List<UploadFile> {
val block: (Realm) -> List<UploadFile> = { realm ->
pendingUploadsQuery(realm).findAll()
}

return customRealm?.let(block) ?: getRealmInstance().use(block)
fun getAllPendingUploadsWithoutPriorityCount(realm: Realm): Long {
return pendingUploadsQuery(realm).count()
}

fun getAllPendingPriorityFilesCount(): Long {
Expand All @@ -236,11 +232,13 @@ open class UploadFile(
return pendingUploadsQuery(realm, folderId, true).findAll()
}

fun getAllPendingUploadsCount(): Int {
return getRealmInstance().use { realm ->
fun getAllPendingUploadsCount(customRealm: Realm? = null): Int {
val block: (Realm) -> Int = { realm ->
realm.refresh() // TODO: (Realm kotlin) - Remove when we update to Realm Kotlin
pendingUploadsQuery(realm).count().toInt()
}

return customRealm?.let(block) ?: getRealmInstance().use(block)
}

fun getCurrentUserPendingUploadsCount(folderId: Int? = null): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.infomaniak.drive.data.models.AppSettings
import com.infomaniak.drive.data.models.MediaFolder
import com.infomaniak.drive.data.models.SyncSettings
import com.infomaniak.drive.data.models.UploadFile
import com.infomaniak.drive.data.models.UploadFile.Companion.getRealmInstance
import com.infomaniak.drive.data.services.UploadWorkerThrowable.runUploadCatching
import com.infomaniak.drive.data.sync.UploadNotifications
import com.infomaniak.drive.data.sync.UploadNotifications.showUploadedFilesNotification
Expand Down Expand Up @@ -149,12 +150,16 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor
}

private suspend fun startSyncFiles(): Result = withContext(Dispatchers.IO) {
val uploadFiles = UploadFile.getAllPendingUploads()
pendingCount = uploadFiles.size
var uploadFiles: List<UploadFile>

if (pendingCount > 0) applicationContext.cancelNotification(NotificationUtils.UPLOAD_STATUS_ID)
getRealmInstance().use { realm ->
uploadFiles = UploadFile.getAllPendingUploads(realm)
pendingCount = uploadFiles.size

checkUploadCountReliability()
if (pendingCount > 0) applicationContext.cancelNotification(NotificationUtils.UPLOAD_STATUS_ID)

checkUploadCountReliability(realm)
}

SentryLog.d(TAG, "startSyncFiles> upload for ${uploadFiles.count()}")

Expand Down Expand Up @@ -184,10 +189,10 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor
if (uploadedCount > 0) Result.success() else Result.failure()
}

private fun checkUploadCountReliability() {
val allPendingUploadsCount = UploadFile.getAllPendingUploadsCount()
private fun checkUploadCountReliability(realm: Realm) {
val allPendingUploadsCount = UploadFile.getAllPendingUploadsCount(realm)
if (allPendingUploadsCount != pendingCount) {
val allPendingUploadsWithoutPriorityCount = UploadFile.getAllPendingUploadsWithoutPriority().count()
val allPendingUploadsWithoutPriorityCount = UploadFile.getAllPendingUploadsWithoutPriorityCount(realm)
Sentry.withScope { scope ->
scope.setExtra("uploadFiles pending count", "$pendingCount")
scope.setExtra("realmAllPendingUploadsCount", "$allPendingUploadsCount")
Expand Down
Loading