0

I am working on an android project and I've been trying to use Firebase Firestore collections. Everything seems fine up until the second activity retrieving the intent with the parsed list of watches.

public class MainActivity extends AppCompatActivity {
  private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference watchesRef = db.collection("watches");
    private List<Watch> watches = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        watchesRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        Watch watch = document.toObject(Watch.class);
                        watches.add(watch);
                    }
                    Intent intent = new Intent(MainActivity.this, WatchesActivity.class);
                    intent.putParcelableArrayListExtra("watches", (ArrayList<Watch>) watches);
                    startActivity(intent);
                } else {
                    Log.d("MainActivity", "Error getting documents: ", task.getException());
                }
            }
        });

Say I wanted to print out each watch upon retrieval from firstore, it works just fine. When debugging, I can see the list getting filled with the watches. Debugging goes through to the next activity:

    Intent intent = getIntent();
        if (intent.hasExtra("watches")) {
     watches = intent.getParcelableArrayListExtra("watches");
        }

Right up until watches = intent.getParcelableArrayListExtra("watches"); I can see the data just fine, then I get this error java.lang.RuntimeException: Parcel android.os.Parcel@f983085: Unmarshalling unknown type code 7536745 at offset 316

KEEP IN MIND: When I clear the list then add static data, it works just fine.

Nour
  • 1
  • 2

1 Answers1

0

Overall your approach to Android app architecture is suffering here, fetching a potentially large list of data and then attempting to pass it via an Intent to another Activity is an architecture that won't scale well.

It may fail when the parcel data size exceeds 1MB.

It may fail because Watch doesn't use only primitives or hasn't implemented itself well as Parcelable.

It may fail because when the user opens that Activity, the Watch list is so old and out-dated that your updated app crashes.

A better way to architect the app is to have the Activity that you navigate to fetch the data it needs itself.

If/when you use Intents, keep the payloads minimal. Try to pass just a single id with which the recipient can retrieve from local database or fetch from the Internet with.

straya
  • 5,002
  • 1
  • 28
  • 35
  • Watch class implements parcelable just fine with all the necessary methods. It's a university project so the data is fairly little. As I mentioned, when I send the same list statically, no issue occurs. – Nour Feb 13 '23 at 10:52