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

[WIP] GRDB Migration #683

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

mohannad-hassan
Copy link
Collaborator

First draft: shows a working example for a GRDB stack for page bookmarks. Tests will be similar to the CoreData variant, except that the GRDB's integration with Combine doesn't guarantee that publishers will signal every time the resources is mutated, instead it might aggregate a few mutations together.

Also, still remains to test the publisher function in DatabaseConnection.

try db.create(table: GRDBPageBookmark.databaseTableName, options: .ifNotExists) { table in
// Don't think we need a separate local id column.
table.column("page", .integer).primaryKey()
table.column("remote_id", .text)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think This should be non-null, since PageBookmarks table would be the persistence for synchronized information and doesn't hold local mutations.

import VLogging
import SQLitePersistence

public struct GRDBPageBookmarkPersistence: PageBookmarkPersistence {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it needs to conform to PageBookmarkPersistence. PageBookmarkPersistence is basically the repository (the pattern) API. Now GRDBPageBookmarkPersistence is not going to be used directly, instead it will be just a data source. This allows us to be able to supply a tailored entity that includes the remote ID as part of it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually operating on the design that it's going to replace bookmarks' CoreDataPersistence in the app.

  • At the launch of the next update, data is migrated from CoreData to this one.
  • GRDBPageBookmarkPersistence is going to be the source of truth as far the app's presentation is concerned.
  • Some other storage will be created to handle sync-related concerns. i.e. local changes, last synced date, ... This is most likely going to be transient; once synced, local changes are going to be wiped and last synced date updated.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see two things we need that would make GRDBPageBookmarkPersistence not applicable to be the public API:

  1. GRDBPageBookmarkPersistence only holds synchronized data. We need an additional persistence layer to store offline mutations that haven't been synchronized with the server yet (e.g., GRDBPageBookmarkMutationsPersistence).
  2. We need a higher-level class to manage interactions between different data sources (GRDBPageBookmarkPersistence, GRDBPageBookmarkMutationsPersistence, and Quran.com RemoteDataSource). This is typically implemented using the repository pattern. See an example here:https://developer.android.com/topic/architecture/data-layer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get your point, and it makes sense.

Comment on lines +113 to +121
/// Creates a publisher that tracks changes in the results of database requests,
/// and notifies fresh values whenever the database changes
///
/// The first value is notified when the publisher is created. Subsequent changes *may* get coalesced in notifications.
public func readPublisher<T>(_ block: @Sendable @escaping (Database) throws -> T) throws -> AnyPublisher<T, Error> {
// - ValueObservation may coalesce subsequent changes into a single notification
// - ValueObservation fetches a fresh value immediately after a change *is committed in the database*.
// - By default, delivers on the main thread.
ValueObservation
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mohamede1945 See here and see the test below.

I've pulled the inner comments from the documentation of ValueObservation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Thank you!

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

Successfully merging this pull request may close these issues.

2 participants