0

I have the following requirement:

  1. I wanted to get a long string from internet
  2. Store it locally
  3. Notify user about this

For step 1, I have the following repository functions:

fun getDataFromInternet() = flow<String> {
    // emits the data string from internet, else empty string
}

fun getString() = flow<String> {
    getDataFromInternet().collect{
        if (it.isNotEmpty()){
            appDao.insertString(it)
            // after successful execution of the above insert I want to emit("got data") or emit("") otherwise
        }
    }
}

For step 2, I have a DAO with Room's Insert function:

@Dao

interface AppDAO {
    ...
    ...
    
    @Insert
    suspend fun insertString(largeString: String): Long
    ...
    ...
}

But since the insertString() function is a suspend function how can I ensure that the insertion is completed, before I move step 3?

In the documentation it states

If the @Insert method receives a single parameter, it can return a long value, which is the new rowId for the inserted item.

Why does it say can in the statement? This is another confusion.

Mr. Techie
  • 622
  • 7
  • 17

0 Answers0