-
Notifications
You must be signed in to change notification settings - Fork 211
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
Improve database container read and reset #3365
Conversation
) | ||
extraData = [:] | ||
var extraData = [String: RawJSON]() | ||
if !dto.user.extraData.isEmpty { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If data is empty, error is logged which can happen when trying to convert deleted DTO
@@ -1154,6 +1154,53 @@ extension NSManagedObjectContext: MessageDatabaseSession { | |||
$0.localState = .pendingUpload | |||
} | |||
} | |||
|
|||
func loadMessage( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved these under DatabaseSession so that I can use DatabaseSession in async version of the read()
let changeCounts = context.currentChangeCounts() | ||
let results = try actions(context) | ||
if context.hasChanges { | ||
assertionFailure("Background context is read only, but calling actions() created changes") | ||
if changeCounts != context.currentChangeCounts() { | ||
assertionFailure("Context is read only, but actions created changes: (updated=\(context.updatedObjects), inserted=\(context.insertedObjects), deleted=\(context.deletedObjects)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did not figure out why deleted message is kept tracked by the context which triggered this assert. Just changed it around so that we only trigger it when actually the actions closure changed something.
// Finally reset states of all the contexts after batch delete and deletion propagation. | ||
if let writableContext = self?.writableContext, let allContext = self?.allContext { | ||
writableContext.invalidateCurrentUserCache() | ||
writableContext.reset() | ||
|
||
for context in allContext where context != writableContext { | ||
context.performAndWait { | ||
context.invalidateCurrentUserCache() | ||
context.reset() | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved these here because context should be reset after we are done with the batch delete. Otherwise deletes stick around to contexts (because I added NSManagedObjectContext.mergeChanges
above). Better to keep them clean when dealing with logout
SDK Size
|
Generated by 🚫 Danger |
SDK Performance
|
// Finally reset states of all the contexts after batch delete and deletion propagation. | ||
if let writableContext = self?.writableContext, let allContext = self?.allContext { | ||
writableContext.invalidateCurrentUserCache() | ||
writableContext.reset() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you also tried reseting these context when a delete is performed on write
function? (Not sure if this will do anything tho)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did not test that. Slightly afraid to do it because we use these contexts for DB observers. Thinking about letting it be 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah probably better 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, I tested it and it works. Maybe if we still have problems with this in the future we can go back here and apply the following change before the CatabaseContainer.write
:
if !self.backgroundReadOnlyContext.deletedObjects.isEmpty {
self.backgroundReadOnlyContext.reset()
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! ✅
… where on logout contexts are referencing DTOs
befe2cf
to
1736ff7
Compare
SDK Size
|
Quality Gate passedIssues Measures |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
🔗 Issue Links
Resolves PBE-5422
🎯 Goal
Fix issues around database read wrappers and context resets
📝 Summary
🧪 Manual Testing Notes
Case 1:
Log in and log out
Case 2:
☑️ Contributor Checklist