Skip to content

Commit

Permalink
Merge pull request #1081 from claraf3/pagingWithNetworkSample-cleanUp
Browse files Browse the repository at this point in the history
Clean up query order logic in PagingWithNetwork sample
  • Loading branch information
claraf3 committed Mar 18, 2023
2 parents d654ce4 + 7f08727 commit a875356
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
Expand Up @@ -28,12 +28,10 @@ interface RedditPostDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAll(posts: List<RedditPost>)

@Query("SELECT * FROM posts WHERE subreddit = :subreddit ORDER BY indexInResponse ASC")
// Order by _id which is an immutable key to help maintain consistent item ordering
@Query("SELECT * FROM posts WHERE subreddit = :subreddit ORDER BY _id ASC")
fun postsBySubreddit(subreddit: String): PagingSource<Int, RedditPost>

@Query("DELETE FROM posts WHERE subreddit = :subreddit")
suspend fun deleteBySubreddit(subreddit: String)

@Query("SELECT MAX(indexInResponse) + 1 FROM posts WHERE subreddit = :subreddit")
suspend fun getNextIndexInSubreddit(subreddit: String): Int
}
Expand Up @@ -32,9 +32,9 @@ class PostFactory {
created = System.currentTimeMillis(),
thumbnail = null,
subreddit = subredditName,
url = null
url = null,
_id = id
)
post.indexInResponse = -1
return post
}
}
Expand Up @@ -25,7 +25,11 @@ import com.google.gson.annotations.SerializedName
@Entity(tableName = "posts",
indices = [Index(value = ["subreddit"], unique = false)])
data class RedditPost(
@PrimaryKey
// A unique Int key starting at 0 which autoIncrements by 1 with every insert.
// We need this immutable key to maintain consistent item ordering so that the UI
// does not scroll out of order when new posts are appended and inserted into this db.
@PrimaryKey(autoGenerate = true)
val _id: Int?,
@SerializedName("name")
val name: String,
@SerializedName("title")
Expand All @@ -43,6 +47,4 @@ data class RedditPost(
val created: Long,
val thumbnail: String?,
val url: String?) {
// to be consistent w/ changing backend order, we need to keep a data like this
var indexInResponse: Int = -1
}

0 comments on commit a875356

Please sign in to comment.