# Bug 3456: Weird behavior of 'complete' when unloading/loading same query quickly

- Status: closed
- Project: Zero
- Creator: @aboodman
- Labels: zero
- Created: 2025-01-21T06:32:03Z
- Modified: 2025-01-24T10:22:04Z
- URL: https://bugs.rocicorp.dev/p/zero/issue/3456

## Description

See video description here: https://youtu.be/1iUrac6dw_Q

Reproduction: https://github.com/aboodman/zero-ui-flicker

Also there's a secondary weird thing where if you change the `kvStore` type to `idb` the behavior changes. I don't expect it to change at all - I expect the idb kvStore to unload and resync the queries in the same way the memstore does. But it doesn't.

User report here: 

https://discord.com/channels/830183651022471199/1322102519987572747/1331140390979637281

## Comments (6)

### @arv — 2025-01-21T17:22:54Z

The difference between `mem` and `idb` is most likely the latency introduced by IDB.

### @arv — 2025-01-21T17:23:18Z

The thing that happens is that the queries gets unregistered and we get `del` for all the rows before the new query comes in. This triggers the `[], complete` result.

I think we need to ensure we unregister the useQuery listener before we unregister the query so we do not see the result of the query being empty.

### @aboodman — 2025-01-21T17:34:25Z

Aha, makes sense!

### @arv — 2025-01-22T09:31:13Z

@tantaman added the delayed unregister in https://github.com/rocicorp/mono/pull/3579 which fixes the `[], complete` case.

https://youtu.be/FVGJSDY7AVE

### @arv — 2025-01-22T09:35:12Z

With `store: 'idb'` the result now makes sense.

The log for **mem** is:

```
Constructing a new Zero instance!
page.tsx:11 todos1 [] unknown
page.tsx:11 todos1 [] unknown
page.tsx:11 todos1 (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] complete
page.tsx:11 todos2 (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] complete
```

The log for **idb** is:

```
Constructing a new Zero instance!
page.tsx:11 todos1 [] unknown
page.tsx:11 todos1 (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] complete
page.tsx:11 todos2 (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] complete
```

Which is slightly better since we already have the data.

I'll look into why we get 2 `[] unknown` for the **mem** case.

### @arv — 2025-01-24T10:22:04Z

Fixed
