Convert to JSON class objects that recur reference

Asked

Viewed 235 times

1

I am trying to convert to JSON two lists of dististe classes.

public class CustomerItem implements Parcelable {
    //...
    private List<ProductItem> mProductList = new ArrayList<>();
...
}

public class ProductItem implements Parcelable {
   //...
    private List<String> mCustomerList;
//...
}

And I have the following function onPause():

@Override
    protected void onPause() {
        Gson gson = new Gson();
        mJSONProducts = gson.toJson(mBillAccount.getProductItemList());
        mJSONCustomers = gson.toJson(mBillAccount.getCustomerItemList());
        //mBillAccount é objeto da classe BillAccount, que contém as duas listas como membro
        mEditor.putString(KEY_PRODUCT_LIST, mJSONProducts);
        mEditor.putString(KEY_CUSTOMER_LIST, mJSONCustomers);
        /...
        mEditor.apply();
        super.onPause();
    }

Within my function onPause(), i convert the lists with the method gson.toJson(), to be able to rescue them when restarting the app. At this moment, according to the memory usage monitor, the app is increasingly consuming the memory of the phone.

With the use of Debugger, I noticed that no value pass is ever invoked with the method mJSONCustomers = gson.toJson(mBillAccount.getCustomerItemList()); When reopening the app, the GUI freezes (I believe it freezes because it is still trying to generate JSON). When opening memory usage monitor, I noticed that from that instant the app gradually consumes the disk memory. I can give up saving the limb mCustomerList. I tried using the notation @Expose(serialize = false, deserialize = false) on this member, but also failed to work.

EDITION: When I asked the question, I believed it was a cyclical recursion, as if mCustomerList classmate List<CustomerItem> instead of List<String>. It was my lack of attention. mCustomerList is of class List<String>. So I believe the problem is not cyclical recursion as I stated earlier. I apologize for the confusion.

  • Try using this lib (Logansquare), she spends time on compilation, instead of using time execution, because it uses very little of reflection, and the issue of the graphical interface that hangs, you must do this procedure in another thread, different from the main.

  • 1

    @Marcogiovanni believes that the problem is not well performance per se. But the fact that these two classes have a cyclic dependency, which generates an "infinite recursion" when serializing the objects.

  • 1

    @Wakim did not see where is the cyclical dependency, I saw that there is the ProductItem and the CustomerItem only has a list of ProdutcItem, the class ProdutectItem makes no reference to CustomerItem.

  • 1

    I think it was a typo, because the type is String, but the field name is mCustomerList, maybe the OP can remove this doubt. I really guided myself by the report of the OP, but I could be wrong.

  • The good news is I went by code and I already did it with Logansquare’s lib and I had no problem, so I quoted her there.

  • I corrected the question, I apologize a thousand for the confusion with the class.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.