OutdatedGuy / firebase_pagination

A flutter package to paginate Realtime Database and Firestore with real-time updates.
https://pub.dev/packages/firebase_pagination
BSD 3-Clause "New" or "Revised" License
13 stars 11 forks source link

[BUG] Excessive number of read operations from Firestore. #65

Open MohammadAlieye opened 1 month ago

MohammadAlieye commented 1 month ago

Describe the bug

When using the FirestorePagination widget with isLive: true, the app performs an excessive number of read operations from Firestore, causing potential performance issues.

Package version: 4.0.1 Platform: ios and android

My Code Expanded( child: FirestorePagination( query: FirebaseFirestore.instance .collection('Chat') .doc(controller.newMessage!.threadId) .collection('Messages') .orderBy('createdAt', descending: true), limit: 20, isLive: true, shrinkWrap: true, reverse: true, controller: controller.scrollController, itemBuilder: (context, snapshot, index) { print('snapstot=================================='); print(snapshot.length);

                    final messageData =
                        snapshot[index].data() as Map<String, dynamic>;
                    final messageModel = MessageModel.fromMap(messageData);

                    bool showDateHeader = false;
                    bool showUnreadDivider = false;

                    if (index == snapshot.length - 1) {
                      showDateHeader = true;
                    } else if (!ConstantFunction().isSameDay(
                        messageModel.createdAt!.toDate(),
                        (snapshot[index + 1].data()
                                as Map<String, dynamic>)['createdAt']
                            .toDate())) {
                      showDateHeader = true;
                    }

                    if (index ==
                            controller.newMessage!.unreadMessages! - 1 &&
                        controller.newMessage!.receiverId ==
                            StaticData.userModel!.userId &&
                        controller.newMessage!.unreadMessages! >= 1) {
                      showUnreadDivider = true;
                    }

                    if (index == 0 &&
                        messageModel.receiverId ==
                            StaticData.userModel!.userId &&
                        messageModel.unreadMessages! >= 1) {
                      FirebaseChatService().resetMessageCountBadge(
                        messageModel: controller.newMessage!,
                      );
                      controller.newMessage!.unreadMessages = 0;
                    }

                    return Column(
                      crossAxisAlignment: messageModel.senderId ==
                              StaticData.userModel!.userId
                          ? CrossAxisAlignment.end
                          : CrossAxisAlignment.start,
                      children: [
                        if (showDateHeader)
                          Padding(
                            padding:
                                const EdgeInsets.only(top: 10, bottom: 10),
                            child: Center(
                              child: Text(
                                ConstantFunction().formatDateHeader(
                                  messageModel.createdAt!.toDate(),
                                ),
                                style: TextStyle(
                                    color: Colors.grey, fontSize: 14),
                              ),
                            ),
                          ),
                        MessageBubble(
                          messageModel: messageModel,
                          index: index,
                        ),
                      ],
                    );
                  },
                  onEmpty: Center(
                    child: Text("No chat yet"),
                  ),
                ),
              )

Expected behavior

The FirestorePagination widget should efficiently load and display paginated messages without causing excessive read operations when isLive: true is enabled.

Actual behavior

When isLive: true is enabled, the widget performs an excessive number of Firestore reads, which can negatively impact app performance and incur higher Firestore read costs.

Screenshots/Code Snippets

In just one hour after implementation of this package. sds

Additional context

This issue is observed on both iOS and Android platforms.

OutdatedGuy commented 4 days ago

Hi @MohammadAlieye, currently I'm not actively maintaining this package. Can you try using the official firestore pagination package?