[Fix] Model store saving to and loading from prefs, and remove duplicates#2099
[Fix] Model store saving to and loading from prefs, and remove duplicates#2099
Conversation
|
Code looks good and the test works! Should we consider removing the duplicates since they are most likely unnecessary in the event that a duplicate scenario occurs? |
Can you explain what you mean? This PR prevents a duplicate operation from being added. Are you talking about the model store's remove method? |
Sure. If the user already has duplicate operations in the cache from migrating from 5.1.12, the saved operations will not be added to the model store but will remain in the cache. I am thinking we can add an extra step to remove the duplicates if they are detected and ensure a cleaner migration. |
I see, I don't think another check or removal from cache is necessary or fixes the issue because of the following reasons:
|
jkasten2
left a comment
There was a problem hiding this comment.
@nan-li Nice catch here! Didn't think about the case where OperationRepo._operationModelStore would still contain the duplicate.
Another question comes to mind now, do we need to keep the duplicate check in internalEnqueue as well? I am thinking yes, since we still need to ensure it doesn't get put into OperationRepo.queue, where it would be processed during the currently runtime.
- This problem however does point to a duplicate state complexity we have in our system. Something that would be nice to clean up in the future, if possible.
Lastly, could we add a test before merging this in?
938a7cc to
b758b34
Compare
|
Thinking about this again the root of the problem is |
b758b34 to
691cdd8
Compare
Yes we still need the check in the Operation Repo. The operations are already enqueued to the Op Repo before
This is a good point, I agree. I checked that the cache should always be updated and correct, so it is safe to do a full replacement. I updated the PR. |
691cdd8 to
50e7db4
Compare
jkasten2
left a comment
There was a problem hiding this comment.
LGTM! Much simpler logic now!
|
Thinking again about the model store reading from cache, I realized that it will almost always lose old cached operations...
|
|
I see, so this isn't just a problem with |
50e7db4 to
5cf77d2
Compare
fd39e7d to
f3c4bdb
Compare
|
Testing again, I think the I have only been able to reproduce with the creation of the initial anonymous user, as those operations enqueue really early in |
* Migration fix for bug introduced in 5.1.12 * The following check is intended for the operation model store. * When the call to this method moved out of the operation model store's initializer, duplicate operations could be cached. * See #2099
f3c4bdb to
40d9d7e
Compare
23ff508 to
40d9d7e
Compare
| return | ||
| } | ||
|
|
||
| val shouldRePersist = models.isNotEmpty() |
There was a problem hiding this comment.
This should be in the synchronized(models) {} below to prevent race conditions and concurrent modification expectations.
Background: * The Operation Model Store was previously adding duplicate operations when the same ones that were previously added to the store and persisted, are re-read from cache. * In addition, any operations enqueued before load is called overwrote the cache, so old cached operations are lost. Solution: * Don't persist models before load is called. This is safe because the time gap between any models being added and load being called should be very small. * When load is called, the cached models are added to the front. Then the models get persisted altogether again if necessary.
* Migration fix for bug introduced in 5.1.12 * The following check is intended for the operation model store. * When the call to this method moved out of the operation model store's initializer, duplicate operations could be cached. * See #2099
40d9d7e to
7e05ed3
Compare
* Migration fix for bug introduced in 5.1.12 * The following check is intended for the operation model store. * When the call to this method moved out of the operation model store's initializer, duplicate operations could be cached. * See #2099
Description
One Line Summary
Don't overwrite operation model store cache before it as been loaded, and avoid duplicates when loading them.
Details
Bug Fix
firstOrNullsearch for the operation. Then, on the next cold start, the remaining operation will load from cache and become enqueued to the OperationRepoSolution
Motivation
Bug fix
Testing
Unit testing
Added test that saves duplicates to Prefs, enqueues a model, then loads from cache.
Manual testing
Android emulator on API 33
Reproduce Bug Before PR
login-userand acreate-subscriptionare quickly enqueued and persistedlogin-userandcreate-subscriptionfirstOrNulllogin-userand thecreate-subscriptionare read from cache and added to the Operation RepoAffected code checklist
Checklist
Overview
Testing
Final pass
This change is