Android架构组件 - 从会议室

时间:2017-11-17 06:40:28

标签: android android-room android-architecture-components

目前我正在开发Android应用程序架构,我无法理解Github Repo Sample中的一件事;我们可以决定使用速率限制器或空值检查来获取新数据,但在这种情况下,我们只是将新值插入到数据库中,我们不会从数据库中删除旧值。例如,如果数据库中的旧结果之一从服务器(用户删除的repo)中删除了怎么办?它还会在数据库中吗?我们怎样才能消除这个价值?我错过了什么或这是一个未经处理的案件?

2 个答案:

答案 0 :(得分:4)

我认为我有一个优化且更简单的解决方案。请帮忙使其变得更简单!

// First, convert and add all Json data to lstUsers

// Second, add only Json primary key elements 
// from lstUsers to a new list lstIDUsers

userDao.insertAll(lstUsers);
userDao.deleteOldUsers(lstIDUsers);

//调用Dao的文件-例如:存储库类

using System;
using System.Collections.Generic;
using System.Linq;

namespace Averages
{
    class Program
    {
        static void Main(string[] args)
        {
            //list to hold your numbers, way more flexible than array
            List<double> enteredNubers = new List<double>();

            //message user
            Console.WriteLine("Enter number(s) or 0 to end: ");

            //run this block of code indefinitely
            while (true)
            {
                //take user input
                string userinput = Console.ReadLine().Trim();
                //if user enters 0, exit this loop
                if (userinput == "0")
                    break;


                double num;
                //try to convert text to number
                if (double.TryParse(userinput, out num))
                {
                    //if it is successful, add number to list
                    enteredNubers.Add(num);
                }
                else //else message user with error
                    Console.WriteLine("Wrong input. Please enter number or 0 to end");
            }

            //when loop is exited (when user entered 0), call method that calculates average
            Average(enteredNubers);
            Console.ReadKey();
        }
        static void Average(List<double> numbers)
        {
            double sum = 0;
            //go through list and add each number to sum
            foreach (double num in numbers)
            {
                sum += num;
            }
            //or, you can sum it using linq like this:
            //sum = numbers.Sum();
            //or you can even calculate average by calling Average method on list, like numbers. Average();

            //show message  - all the entered numbers, separated by comma
            Console.WriteLine("You have entered: " + string.Join(", ", numbers.ToArray()));
            //write average
            Console.WriteLine("The average is: " + sum/numbers.Count);
        }
    }
}

这两个命令首先使用OnConflictStrategy.REPLACE插入所有元素,然后仅删除从远程或本地数据库中删除的旧元素。

答案 1 :(得分:2)

Okey我找到了解决方案;

这是示例实体,

@Entity
data class User(
        @PrimaryKey val uid : Int, 
        val name : String, 
        val createdTime : Long = System.currentTimeMillis()
)

及以下是取自https://jsonplaceholder.typicode.com的示例json 并重新格式化。

private val userJson = "[{\"uid\": 1,\"name\": \"Leanne Graham\"},{\"uid\": 2,\"name\": \" Graham\"},{\"uid\": 3,\"name\": \"Y Graham\"},{\"uid\": 4,\"name\": \"Lea\"}]"

通过使用https://github.com/FasterXML/jackson-module-kotlin,您可以轻松地将json格式的数据反序列化为kotlin中的数据类。与gson库不同,jakson支持默认值,因此您可以设置一个当前毫秒的创建时间。以下是UserDao

@Dao
interface UserDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertUser(user : User)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertUsers(user : List<User>)

    @Query("SELECT * FROM user")
    fun getUsers() : List<User>

    @Delete()
    fun deleteOldUsers(users : List<User>)

比使用getUser()方法获取所有用户并将其过滤为旧的和新鲜的。你应该从数据库中删除旧的。如果freshUsers不为空,请使用它,如果为空则发出新的网络请求。

val expireTime = TimeUnit.HOURS.toMillis(1)
val userList = App.database.userDao().getUsers()
val freshUsers = userList.filter { it.createdTime > System.currentTimeMillis() - expireTime}
val oldUsers = userList.filter { it.createdTime < System.currentTimeMillis() - expireTime}
App.database.userDao().deleteOldUsers(oldUsers)

此解决方案适合我。但我也很高兴听你的解决方案。