DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

Saved object returned with null content.

Open janosvig opened this issue 9 years ago • 15 comments

DBFlow Version: 4.0.0-beta5 Issue Kind (Bug, Question, Feature): Bug

Description: We are trying to save in DB a chat message object which contains different fields and two images with their byte code. Most of the time it is working fine but there are some situations, where the object saved successfully (save returns true) is later on returned "empty", meaning that its variables are set to null.


private boolean saveValid(Chat chat) {
        Log.d(Constants.TAG_ARCH, "ChatModel.saveValid: " + chat);
        boolean success = chat.save();

        Chat chat2 = SQLite.select().from(Chat.class).where(Chat_Table.firebaseKey.is(chat.getFirebaseKey())).querySingle();

        Log.d(Constants.TAG, "ChatModel.saveValid: returned chat " + chat2);
        return  success;
    }

This is the log received:

ChatModel.saveValid: ||Chat||com.madeinworld.android.common.chat.db.vo.Chat@361e303|PENDING_IMAGE|[-KiFhJ8YmMtzk9mG7EWI] pending =true, hasImage=true]

ChatModel.saveValid: returned chat ||Chat||com.madeinworld.android.common.chat.db.vo.Chat@b2ac280|MESSAGE_SENT|[null] pending =false, hasImage=false]null

This is how we handle chat image:


 public byte[] getByteThumbnail() {
        if(thumbnail == null){
            return null;
        }
        return thumbnail.getBlob();
    }

    public void setByteThumbnail(byte[] byteThumbnail) {
        this.thumbnail = new Blob(byteThumbnail);;
    }

We are really not sure how or why the issue is happening.

For the moment there were two scenarios:

  1. Sending various images one ofter other, using different threads. Issue happens after 5th or 6th image
  2. Sending images from a specific folder from a phone gallery (e.g.:instagram folder)

Each case the images are shown correctly on GUI prior to sending so the byte code is correctly. Object should be correctly saved. Any help would be appreciated..

Chat.java.txt

janosvig avatar Apr 21 '17 13:04 janosvig

use the database transactions async. You have a threading issue and should perform read and writes on same thread. read the storing and retrieving docs.

agrosner avatar Apr 24 '17 16:04 agrosner

Hi, thank you for answering but I do not really understand your response, could you please explain?

First you say that I should use async. Why is would that help? In my case this is not something what would be desirable because of the logic implemented, where I am already using a separate thread for the management DB operations. Actually I verified that given object is never saved in the db even if I retry retrieval.

As you can see from the code I shared (saveValid) I am using the same function to save and retrieve data so I don't think that would be a different thread.

Also if would be threading issue it would not explain why it is happening with very specific image always, while 99% of the images it is not happening at all.

janosvig avatar Apr 25 '17 10:04 janosvig

So I have tried the following:


public void saveChatAsync(final Chat chat){
        Log.d(Constants.TAG, "NewChatMessageJob.saveChatAsync: " + chat);
        DatabaseDefinition database = FlowManager.getDatabase(MIWDatabase.class);
        Transaction transaction = database.beginTransactionAsync(new ITransaction() {
            @Override
            public void execute(DatabaseWrapper databaseWrapper) {
                Log.d(Constants.TAG, "NewChatMessageJob.saveChatAsync.execute: ");
                chat.save();
            }
        }).success(new Success() {
                    @Override
                    public void onSuccess(Transaction transaction) {

                        Log.d(Constants.TAG, "NewChatMessageJob.saveChatAsync.onSuccess: ");

                        Chat savedChat =  SQLite.select().from(Chat.class).where(Chat_Table.firebaseKey.is(chatMessageKey)).querySingle();

                        Log.d(Constants.TAG, "NewChatMessageJob.saveChatAsync.onSuccess simple retrieval: " + savedChat);

                        SQLite.select()
                                .from(Chat.class)
                                .where(Chat_Table.firebaseKey.eq(chatMessageKey))
                                .async().querySingleResultCallback(new QueryTransaction.QueryResultSingleCallback<Chat>() {
                            @Override
                            public void onSingleQueryResult(QueryTransaction transaction, @Nullable Chat chat) {
                                Log.d(Constants.TAG, "NewChatMessageJob.onSingleQueryResult: " + chat);
                            }
                        }).execute();
                    }
                })
                .error(new Transaction.Error() {
                    @Override
                    public void onError(Transaction transaction, Throwable error) {
                        Log.d(Constants.TAG, "NewChatMessageJob.saveChatAsync.onError: ");
                        // call if any errors occur in the transaction.
                    }
                }).build();
        transaction.execute();
    }

The result is the following:

NewChatMessageJob.saveChatAsync: ||Chat||com.madeinworld.android.common.chat.db.vo.Chat@fcc83d1|PENDING_IMAGE|[-Ki_agIIVYO51W1W-MAv] pending =true, hasImage=true] NewChatMessageJob.saveChatAsync.execute: NewChatMessageJob.saveChatAsync.onSuccess: NewChatMessageJob.saveChatAsync.onSuccess simple retrieval: ||Chat||com.madeinworld.android.common.chat.db.vo.Chat@e8660e6|MESSAGE_SENT|[null] pending =false, hasImage=false]null NewChatMessageJob.onSingleQueryResult: ||Chat||com.madeinworld.android.common.chat.db.vo.Chat@8a18627|MESSAGE_SENT|[null] pending =false, hasImage=false]null

As you see it seems that the object is not returned correctly independently on how saving and retrieval was implemented.

janosvig avatar Apr 25 '17 15:04 janosvig

I also have the same issue and can't store blobs. Using Kotlin 1.1.2 and dbflow 4.0.1.

It doesn't have this issue in the Android emulator running 7.1.1. However on my phone, LG G6 running 7.0.0 it has the issue.

Using stetho on my phone it shows the database having every column in a row as null. Whereas I have another class that doesn't have the blob portion that saves just fine.

blakelee avatar May 16 '17 02:05 blakelee

@blakelee please follow separate issue with as much information as possible. thank you.

agrosner avatar May 16 '17 04:05 agrosner

Hi, we created a separate app to reproduce this issue. I attach the source. The apk. And the specific image which reproduce the incident. Please let us know if we can be in any more help as this issue is important for us.

Steps to follow:

  1. Download the attached image and save it to your device.
  2. Run imagetest on your device.
  3. Open the app and click the camera button
  4. Select any image.
  5. Press the SAVE button -> check in the Log and toast that there is no error.
  6. Return to step 4 -> Click the camera button.
  7. Select the image you downloaded previously
  8. Click the SAVE button -> Check the messages that appear in the Log.
  9. Click the SAVE ASYNC button -> Check the messages that appear in the Log.

img-20170319-wa0044 1

image-test.zip

IMAGE_TEST.apk.zip

janosvig avatar Jun 07 '17 14:06 janosvig

Hi guys! Could we do something else to help you resolve this issue?

janosvig avatar Dec 11 '17 15:12 janosvig

just tested on pixel xl 2 and it seems fine to me.

12-20 08:36:56.080 9761-9761/com.chat.image.test.android D/imagetest: MainActivity.saveImage: byteOriginal= [B@da10077, byteThumbnail= [B@ebba4e4
12-20 08:36:56.081 9761-9761/com.chat.image.test.android D/imagetest: MainActivity.saveImage: chat=||Chat||com.chat.image.test.android.Chat@c5c3118|PENDING_IMAGE|[012345] pending =true, hasImage=true]null
12-20 08:36:56.152 9761-9761/com.chat.image.test.android D/imagetest: MainActivity.saveImage.getChat= ||Chat||com.chat.image.test.android.Chat@b6f46c4|PENDING_IMAGE|[012345] pending =true, hasImage=true]null
12-20 08:37:01.910 9761-9761/com.chat.image.test.android D/imagetest: MainActivity.saveAsyncImage: byteOriginal= [B@da10077, byteThumbnail= [B@ebba4e4
12-20 08:37:01.910 9761-9761/com.chat.image.test.android D/imagetest: MainActivity.saveAsyncImage: chat=||Chat||com.chat.image.test.android.Chat@aaa2aeb|PENDING_IMAGE|[012345] pending =true, hasImage=true]null
12-20 08:37:01.912 9761-9780/com.chat.image.test.android D/imagetest: MainActivity.saveAsyncImage.execute: 
12-20 08:37:04.208 9761-9761/com.chat.image.test.android D/imagetest: MainActivity.saveAsyncImage.onSuccess: 
12-20 08:37:04.232 9761-9761/com.chat.image.test.android D/imagetest: MainActivity.saveAsyncImage.onSuccess simple retrieval: ||Chat||com.chat.image.test.android.Chat@93f628c|PENDING_IMAGE|[012345] pending =true, hasImage=true]null
12-20 08:37:04.975 9761-9761/com.chat.image.test.android D/imagetest: MainActivity.saveAsyncImage.onSingleQueryResult: ||Chat||com.chat.image.test.android.Chat@f938ab6|PENDING_IMAGE|[012345] pending =true, hasImage=true]null

agrosner avatar Dec 20 '17 13:12 agrosner

Did you try with the very same image? We will also try to retest this with the newest lib version and if we can on pixel 2. But I will need to put my hand on that device.

janosvig avatar Dec 20 '17 13:12 janosvig

FYI running on emulator crashes. not the same image, but an image I downloaded myself. I tested sync, then async, then sync again and no issues.

agrosner avatar Dec 21 '17 01:12 agrosner

Hi guys !

I have exactly the same probleme using 4.2.4 : object.save() does save object (and sometimes does not save anything) with null content values. Do you have any clue please ?

mathieudebrito avatar Oct 10 '18 14:10 mathieudebrito

super

Ekymak7 avatar Oct 14 '18 04:10 Ekymak7

Hi guys !

I have exactly the same probleme using 4.2.4 : object.save() does save object (and sometimes does not save anything) with null content values. Do you have any clue please ?

Can you find the reason?

MLLWF avatar Jun 24 '19 08:06 MLLWF

@MLLWF yep ! I looked at the code and saw a try/catch in the DBFlow called functions. So an error occurred but we were not informed (no logs if I remember well). But in reality there was an error thrown saying (in my case) that the query was too large and could not be handled.

I had to use the "new" (at the time) android database Room to actually understand the issue and solve it. (I switched most of my DBFlow code to Room).

Hope it helps :p

mathieudebrito avatar Jun 24 '19 09:06 mathieudebrito

closing due to inactivity

agrosner avatar Feb 16 '21 00:02 agrosner