vendredi 22 janvier 2021

Kotlin if with inlined method call and not null check goes in if body even if value is null

I have manager that call repository to get value from database.

downloadTaskManager.getTask(taskId:Long):DownloadTask? = downloadTaskRepository.get(taskId)

My repository method is

suspend fun get(id: Long): DownloadTask? = downloadTaskPersistenceDao.get(id)?.defaultDomainTransform()

In one thread I delete a record from database and then in another thread at some moment of time same record is being checked for presence in database.

If I use

if(downloadManager.getTask(downloadId)!=null){
    enqueueDownload(downloadId) // Line 1
}

then Line 1 is being executed even if record is deleted from database previously

But if I use

val currentTask = downloadManager.getTask(downloadId)
if(currentTask!=null){
    enqueueDownload(downloadId) // Line 1
}

then here Line 1 is not being executed.

What might be the difference between these two variant that gives different results?

Aucun commentaire:

Enregistrer un commentaire