Delark
Delark

Reputation: 1323

Android : Why The date data is incorrect ? when retrieved data from Database

When fetching an object from the database it has different long date from before insertion.

My insertion code:

@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);
}

The observer method:

    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");
                            }

                        }


                    }
            );

I want for the code to only be executed in a 1000 millisecond lapse between last edition and now, to prevent any recalculation in the database, the problem is that the long stored in the database is wrong by 2735105 milliseconds which is 45.58 minutes.

The logs:

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

The insertion occurs at 1594952285000. got it from the "insertContent: now is: " + now Log inside the insertContent() method.

By the time it passes the condition if (antiSpammingBoolean && content != null) { it has passed 84 milliseconds (my testing phone is slow), that means that it should go straight to the "content was edited within the first 1000 milliseconds" log, but the content.getLast_edited(); is giving me 1594949549895 which would be a difference of 45 minutes between the first long now = System.currentTimeMillis(); and the content.setLast_edited(now); and that is impossible.

Whats happening here?

EDIT: I did another test and the long is being changed inside the database:

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);
}

This brings the Logs:

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

Which shows the long is still the same inside the Content object before entering the database.

But when withdrawing it, it has changed by 100.56 minutes, which weirdly enough, if we subtract this from the last time I inserted an object in the DB, I think is the real time it took me between both tests (100.58 - 45.58) 55 minutes.

Upvotes: 0

Views: 44

Answers (1)

Delark
Delark

Reputation: 1323

I solved the mistery... I was inserting Objects from a different side of the app where Folder Id was always 0... and my Query was done so that only the objects inserted in a folder where to be calculated... I forgot about that...

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

So everything was working as intentded... even the method I wrote myself says that LMAO

Upvotes: 1

Related Questions