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

Updating HitsTableViewController #157

Closed
rayfarer opened this issue Dec 29, 2020 · 0 comments
Closed

Updating HitsTableViewController #157

rayfarer opened this issue Dec 29, 2020 · 0 comments

Comments

@rayfarer
Copy link

rayfarer commented Dec 29, 2020

What is the best way to update HitsTableViewController after an add/delete/edit? Say that I have an index of items, and one of them is deleted from Algolia. I can use searcher.search() to force a reload, but this doesn't seem to always work.

For instance, I'll delete an item through the Swift iOS API call and see the output below in the console after getting a response and then triggering searcher.search()

2020-12-29T12:54:30-0600 info com.algolia.InstantSearchCore : InstantSearch.SingleIndexSearcher: received results - index: LXIUfjLYsWgtRJGbdSsMluTr9GH3 query: "" hits count: 27 in 1ms

This is the correct count (there were 28 before the delete, so now there are 27), but when I return to the HitsTableViewController, the item I deleted is (sometimes, not always) still there. So I'm having trouble syncing the UI with the data available and creating predictive behavior. I don't want a user to see an item they just deleted. When I perform a blank search it usually gets rid of the old item and the display matches up with the data, but this is an additional unnecessary step I don't want a user to have to do. Not to mention it uses up two operations to do what one should do.

I found this post and also this that asks a similar question (granted it's for a much older version of the library), and @spinach suggests that forcing a reload is necessary. He also mentions registering with Instant Search, and I was wondering how that's done in the current version.

I tried something like this, and it only worked on my newer device:

searcher.onResults.subscribe(with: self) { (_, results) in
		print(results.hits.count)
		if results.hits.isEmpty {
			self.showNoResultsUI()
			print("No results UI")
		 } else {
			print("There are results")
			self.showHits()
		}
	 }
     ...

   func showHits() {
	DispatchQueue.main.async {
	self.filterButton.isEnabled = true
	self.welcomeCollectionView.removeFromSuperview()
	self.noResultsView.removeFromSuperview()
	self.stackView.addArrangedSubview(hitsTableController.tableView)
	hitsTableController.tableView.reloadData()
	}
}

My question: is searcher.search() the best way to do this? For me, it seems to work sometimes, but other times does not. It depends on the device I'm using. On older devices, it's not very reliable. Are there any methods I'm not utilizing that I should be?

My theory is that reloadData() is being called before the data is actually available or faster than the device can process it, hence why on my new iPhone everything works as expected, but on my 5 year old iPad, it doesn't.

RESOLVED: Wow, I didn't search very well. @VladislavFitz answered this exact problem yesterday here. All is well. Something like the code below works great and solves the race condition for the older device.

		hitsInteractor.onResultsUpdated.subscribe(with: self) { _, results in
			DispatchQueue.main.async {
			print("Interactor Test")
			hitsTableController.tableView.reloadData()
			}
				}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant