android - Android:为什么日期数据不正确?从数据库中检索数据时
问题描述
从数据库中获取对象时,它的长日期与插入前不同。
我的插入代码:
@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: now is: " + now
insertContent() 方法中的 Log 获取。
当它通过条件时if (antiSpammingBoolean && content != null) {
,它已经过了 84 毫秒(我的测试手机很慢),这意味着它应该直接进入"content was edited within the first 1000 milliseconds"
日志,但是content.getLast_edited();
给了我 1594949549895,这将是第一个long now = System.currentTimeMillis();
和第一个之间 45 分钟的差异content.setLast_edited(now);
这是不可能的。
这里发生了什么事?
编辑:我做了另一个测试,数据库内部的 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分钟。
解决方案
我解决了这个问题...我从应用程序的另一侧插入对象,其中文件夹 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
推荐阅读
- r - 使用 multidplyr 进行矢量化不会呈现正确的输出
- php - 使用用户输入的 SQL 过滤器(如果提供)
- google-apps-script - 自定义函数:返回值不同于 setValues()
- php - 使用正则表达式同时替换和删除字符
- jenkins - 管道中的 Jenkins 2.x 从代理变量
- mercurial - 如何避免在没有进化扩展的情况下对客户端进行反复过时的警告
- primefaces - p:autocomplete 在通过 ajax 更新父级后停止工作
- python - 适用于返回所有无的数据框
- git - 为什么不会从日志中省略引入相同更改的合并提交?
- c# - Ctrl+单击类时,Visual Studio 会打开其他拆分窗口