Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It is not updated immediately. #58

Open
Patrick386 opened this issue Dec 24, 2022 · 2 comments
Open

It is not updated immediately. #58

Patrick386 opened this issue Dec 24, 2022 · 2 comments
Labels
question Further information is requested

Comments

@Patrick386
Copy link

When you add or modify record, it is not updated immediately.
Can you improve this?
Thank you.

flutter web
algolia_helper_flutter: ^0.2.3


  late HitsSearcher hitSearcher;
  final TextEditingController searchText = TextEditingController();
  PagingController<int, LabelData> pagingController =
      PagingController(firstPageKey: 0);
  Stream<HitsPage> get _searchPage =>
      hitSearcher.responses.map(HitsPage.fromResponse);

  @override
  void initState() {
    /// Algolia DB 검색
    Account account = ref.read(Dependency.account);
    hitSearcher = HitsSearcher.create(
        applicationID: AlgoliaCredentials.applicationID,
        apiKey: account.securedKey!,
        state: const SearchState(
          indexName: AlgoliaCredentials.addressLabelIndex,
          //query: '',
        ));

    searchText.addListener(() {
      hitSearcher.query(searchText.text);
      pagingController.refresh();
    });

    _searchPage.listen((HitsPage page) {
      if (page.pageKey == 0) {
        pagingController.refresh();
      }
      pagingController.appendPage(page.items, page.nextPageKey);
    }).onError((error) => pagingController.error = error);

    pagingController.addPageRequestListener((int pageKey) {
      /// data fetch
      hitSearcher
          .applyState((SearchState state) => state.copyWith(page: pageKey));
    });
    super.initState();
  }

  @override
  void dispose() {
    hitSearcher.dispose();
    searchText.dispose();
    super.dispose();
  }

  Future<void> _refresh() async {
    hitSearcher.query('');
    Future.delayed(const Duration(seconds: 1), () {
      pagingController.refresh();
    });

    if (mounted) {
      return Future<void>.value();
    }
  }

 @override
  Widget build(BuildContext context) {
.....

              PagedSliverList<int, LabelData>(
                        pagingController: pagingController,
                        builderDelegate: PagedChildBuilderDelegate<LabelData>(
                          noItemsFoundIndicatorBuilder: (_) =>
                              NoItemFoundMessageView(),
                          itemBuilder: (BuildContext context, LabelData item,
                                  int index) =>
                              Center(
                            child: SizedBox(
                              width: 700.0,
                              child: ListTile(
                                leading: const Icon(FluentIcons.tag_24_regular),
                                title: Text(
                                  item.name,
                                  style: context.titleMedium,
                                ), ),
                            ),
                          ),
                        ),
                      )
2022-12-20.6.16.32.mov
@aallam
Copy link
Member

aallam commented Dec 27, 2022

Hi @Patrick386, the update isn't immediate because write operations are asynchronous.
I also see that you add/edit your records from the frontend! if this is a public page, you should hide your admin API.
Also, to refresh you can now use HitsSearcher.rerun to rerun the last search query.

@aallam aallam added the question Further information is requested label Dec 27, 2022
@Patrick386
Copy link
Author

Hi @Patrick386, the update isn't immediate because write operations are asynchronous. I also see that you add/edit your records from the frontend! if this is a public page, you should hide your admin API. Also, to refresh you can now use HitsSearcher.rerun to rerun the last search query.

Thank you. I'll try to fix it again. [HitsSearcher.rerun]
Repository

  Future<AddressBookData> create({required AddressBookData book}) async{
    String? uid = repoAuth.currentUid;
    if(uid ==null){
      throw 'User is not logged in.';
    }

    DocumentReference<AddressBookData> docRef = addressBookCollectionRef.doc();
    AddressBookData data = book.copyWith(objectID: docRef.id,
        uid: uid,
        createdAt: TimeUtils.nowMillis,
        updatedAt: TimeUtils.nowMillis );
    await addressBookCollectionRef.doc(data.objectID).set(data);

    return data;
  }

Cloud Functions

exports.onCreateAddressBook = functions
    .region("asia-northeast3")
    .firestore.document(addressBookDocPath)
    .onCreate(async (snapshot: FirebaseFirestore.DocumentSnapshot<FirebaseFirestore.DocumentData>, context: any) => {
        console.log(searchClient.listApiKeys());
        await saveAddressBookDocumentInAlgolia(snapshot);
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants