Skip to content

everytime the app will crash because the retreived data is empty #95

@AlanKong99

Description

@AlanKong99

The code below is extracted from GithubRemoteMediator. It did not deal with exceptions. Actually, you need to because everytime if your data retreived from database or network is empty, your app will crash. For example, if I tried to search 'abcsde', the app will crash because the response is empty. (I used the final version of this codelabs doing this search)

LoadType.PREPEND -> {
                ...
                val remoteKeys = getRemoteKeyForFirstItem(state)
                        ?:throw InvalidObjectException("Remote key and the prevKey should not be null")
                ...
            }
            LoadType.APPEND -> {
                ...
                if (remoteKeys == null || remoteKeys.nextKey == null) {
                    throw InvalidObjectException("Remote key should not be null for $loadType")
                }
                ...
            }

Most importantly, the crash is very annoying. So we need to dealt with our exceptions. And you could do it like the code below:

 LoadType.PREPEND -> {

                val remoteKeys = getRemoteKeyForFirstItem(state)
                    ?: return MediatorResult.Error(InvalidObjectException("Remote key and the prevKey should not be null"))// I do not know wehther this is a good description, maybe you could replace it with a new 
              ....
            }
            LoadType.APPEND -> {
                ...
                if (remoteKeys?.nextKey == null) {
                    return MediatorResult.Error(InvalidObjectException("Remote key should not be null for $loadType")) // I do not know wehther this is a good description
                }
                ...
            }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions