【问题标题】:Android : Why The date data is incorrect ? when retrieved data from DatabaseAndroid:为什么日期数据不正确?从数据库中检索数据时
【发布时间】:2020-11-06 19:34:02
【问题描述】:

从数据库中获取对象时,它的长日期与插入前不同。

我的插入代码:

@Insert(onConflict = OnConflictStrategy.REPLACE)
protected abstract void insert(Content content);

public void insertContent(Content content) {
    long now = System.currentTimeMillis();

    content.setDate_of_creation(now);
    content.setLast_edited(now);
    Log.d(TAG, "insertContent: now is: " + now);
    insert(content);
}

观察者方法:

    contentRepository
            .getLastContentEditedAtFolder()
            .observeForever(
                    content -> {
                        Log.d(TAG, "onChanged: content is: " + content);
                        boolean antiSpammingBoolean = selfDefaultingBoolean.getValue();

                        Log.d(TAG, "observeForever: antiSpammingBoolean is: " + antiSpammingBoolean);

                        if (antiSpammingBoolean && content != null) {
                            long now = System.currentTimeMillis();
                            long lastEdited = content.getLast_edited();
                            Log.d(TAG, "observeForever: now is: " + now);
                            Log.d(TAG, "observeForever: lastEdited is: " + lastEdited);
                            if (lastEdited + (long)1000 > now) {

                                Log.d(TAG, "observeForever: content was edited within the first 1000 milliseconds");
                                int folderId = content.getParent_folder_id();

                                Log.d(TAG, "observeForever: folder id is: " + folderId);

                                if (folderId != 0) {
                                    getFolderTotalAndUpdate(folderId);
                                } else {
                                    Log.d(TAG, "observeForever: content does not belong to a folder!!");
                                }

                            } else {
                                Log.println(Log.ERROR, TAG, "observeForever: content was created way before 1000 ms");
                            }

                        }


                    }
            );

我希望代码仅在上一版和现在之间的 1000 毫秒间隔内执行,以防止在数据库中重新计算,问题是存储在数据库中的 long 错误了 2735105 毫秒,即 45.58 分钟.

日志:

D/AbstractContentDao: insertContent: now is: 1594952285000
D/FolderTotalUpdaterView: onChanged: content is: com.example.flyhigh.data.pojos_entities.content.Content@e190ded
D/FolderTotalUpdaterView: observeForever: antiSpammingBoolean is: true
D/FolderTotalUpdaterView: observeForever: now is: 1594952285084
D/FolderTotalUpdaterView: observeForever: lastEdited is: 1594949549895
E/FolderTotalUpdaterView: observeForever: content was created way before 1000 ms

插入发生在 1594952285000。从 insertContent() 方法中的 "insertContent: now is: " + nowLog 获取。

当它通过条件 if (antiSpammingBoolean && content != null) { 时,它已经过了 84 毫秒(我的测试手机很慢),这意味着它应该直接进入 "content was edited within the first 1000 milliseconds" 日志,但 content.getLast_edited(); 给了我 1594949549895第一个 long now = System.currentTimeMillis();content.setLast_edited(now); 之间会有 45 分钟的差异,这是不可能的。

这里发生了什么?

编辑:我做了另一个测试,数据库内部的 long 正在更改:

public void insertContent(Content content) {
    long now = System.currentTimeMillis();

    content.setDate_of_creation(now);
    content.setLast_edited(now);
    Log.d(TAG, "insertContent: now is: " + now);
    Log.d(TAG, "insertContent: last edited is: " + content.getLast_edited());
    insert(content);
}

这带来了日志:

D/AbstractContentDao: insertContent: now is: 1594955583706
D/AbstractContentDao: insertContent: last edited is: 1594955583706
D/FolderTotalUpdaterView: onChanged: content is: com.example.flyhigh.data.pojos_entities.content.Content@15f56a
D/FolderTotalUpdaterView: observeForever: antiSpammingBoolean is: true
D/FolderTotalUpdaterView: observeForever: now is: 1594955583722
D/FolderTotalUpdaterView: observeForever: lastEdited is: 1594949549895
E/FolderTotalUpdaterView: observeForever: content was created way before 1000 ms

这表明在进入数据库之前,Content 对象内部的 long 仍然相同。

但是当撤回它时,它已经改变了 100.56 分钟,这很奇怪,如果我们从我上次在数据库中插入一个对象时减去它,我认为这是我在两次测试之间花费的实际时间(100.58 - 45.58) 55 分钟。

【问题讨论】:

    标签: android sqlite date android-room


    【解决方案1】:

    我解开了谜团…… 我从应用程序的另一侧插入对象,其中文件夹 ID 始终为 0...并且我的查询已完成,因此只有插入到要计算的文件夹中的对象...我忘记了...

    @Query("SELECT * FROM content_table WHERE parent_folder_id != 0 ORDER BY last_edited DESC LIMIT 1")
    public abstract LiveData<Content> getLastContentEditedAtFolder();
    

    所以一切都按预期工作......即使我自己写的方法也说 LMAO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多