简体   繁体   English

如何临时存储部分数据?

[英]How to temporarily store parts of data?

I have parts of data the coming not by order. 我有部分数据不是按顺序来的。 Let me explain : 让我解释 :

User with id 1 sending data ("phone number") but in the same time user with id 78 sending another data ("home address"). ID为1的用户发送数据(“电话号码”),但ID为78的用户同时发送另一个数据(“家庭住址”)。

All the incoming data have the same receiver. 所有输入数据都具有相同的接收器。

When all set of the data per id is set the data would be sent to mysql database and deleted from temporarily storage. 设置完每个ID的所有数据集后,数据将发送到mysql数据库并从临时存储中删除。

Each user id need to fill 6 different information before the complete data is sent. 每个用户ID需要填写6个不同的信息,然后才能发送完整的数据。

So the question is how to to store the temporary data parts using their personal id (without mixing them up) and only then I have all the parts I will proceed to the next task ? 因此,问题是如何使用其个人ID存储临时数据部分 (不将它们混在一起),只有这样我才能将所有数据都进行下一个任务?

Should I use arrayList or something different ? 我应该使用arrayList还是其他的东西?

Edit (answering the duplicate suggestion ): my question is a bit different and the answer there not helping me at all ! 编辑(回答重复的建议):我的问题有点不同,那里的答案根本没有帮助我!

you can create object 'Data' 您可以创建对象“数据”

class Data {

    private param1,param2,...,param6;


    public Data(){

    }

    /* PARAMS getters */

    /* PARAMS setters */


    public void insert(){
        //insert to database
    }

}

then use HashMap to update the data : 然后使用HashMap更新数据:

HashMap<Integer, Data> map = new HashMap<Integer, Data>();

and every time you recive some data and id check the type of the data (which param in the Data object) and update it in the map 并且每次您获取一些数据和id时都要检查数据的类型(Data对象中的哪个参数)并在地图中对其进行更新

Data toUpdate = map.get(id);
toUpdate.setParam...

you can make a listener for each time you set some param to indicate that all the data had been set and ready to be inserted 您可以在每次设置一些参数以表明所有数据都已设置并准备插入时使它成为监听器。

You could, as you say, store it in some structure in memory. 如您所说,您可以将其存储在内存中的某些结构中。 I'd pick some method which isn't tied to an Activity, otherwise you need to track the activity lifecycle. 我会选择一些与活动无关的方法,否则您需要跟踪活动生命周期。 Eg you could have a HashMap to map entity name (ie name, address, etc) to its value (or in case you're storing multiple instances of a single entity, go with some kind of a Data class suggested in the other answer). 例如,您可以使用HashMap将实体名称(即名称,地址等)映射为其值(或者,如果要存储单个实体的多个实例,请在另一个答案中建议使用某种Data类) 。 Personally, I find this method most cumbersome/cluttered, because you need to either divorce your storage from your Activity or handle all activity changes in order not to lose state. 就我个人而言,我发现此方法最繁琐/凌乱,因为您需要将存储与“活动”分开,或者处理所有活动更改,以免丢失状态。

Second approach is using SharedPreferences. 第二种方法是使用SharedPreferences。 Make a separate prefs file for that purpose, obtain them ( getSharedPreferences("partial_data_dl", MODE_PRIVATE) ), store each column as one field in the prefs, and clear them before commiting them to the database. 为此目的制作一个单独的prefs文件,获取它们( getSharedPreferences("partial_data_dl", MODE_PRIVATE) ),将每一列存储为prefs中的一个字段,并在将它们提交到数据库之前清除它们。 It's easier than storing everything in memory and shouldn't be noticeably slower. 这比将所有内容存储在内存中容易,并且不应明显慢。

Third is the obvious one: why don't you update the database record as the data comes in? 第三点是显而易见的:为什么不随数据输入更新数据库记录? First time insert a new record with only one column populated, every other issue an update query to add value for the new column. 第一次插入仅填充一列的新记录,每隔一次发出更新查询以为新列添加值。 This is admittedly the heaviest solution and might not work for your use case, but I'd give it a try and test it out. 诚然,这是最重的解决方案,可能不适用于您的用例,但我会尝试一下进行测试。 As the saying goes, premature optimization is the root of all evil. 俗话说,过早的优化是万恶之源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM