Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Feat offline support body #334

Merged
merged 7 commits into from
Mar 13, 2023
Merged

Conversation

gewenyu99
Copy link
Contributor

@gewenyu99 gewenyu99 commented Feb 10, 2023

What does this PR do?

Adds body to offline support PR.

Details to be filled are marked TODO.

Test Plan

(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work.)

Related PRs and Issues

(If this PR is related to any other PR or resolves any issue or related to any issue link all related PR and issues here.)

Have you read the Contributing Guidelines on issues?

(Write your answer here.)

@gewenyu99 gewenyu99 requested a review from stnguyen90 February 10, 2023 21:25
Copy link
Contributor

@stnguyen90 stnguyen90 left a comment

Choose a reason for hiding this comment

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

Sounds good so far. Just some tweaks.

Co-authored-by: Steven <1477010+stnguyen90@users.noreply.github.com>
@gewenyu99
Copy link
Contributor Author

Proposing additions to getting started guides later.

Copy link
Contributor

@stnguyen90 stnguyen90 left a comment

Choose a reason for hiding this comment

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

I added the content you requested. Let me know if there's anything else you need.

You should consider enabling Offline Support if you expect the device running your application to experience short periods without a network connection, like going on an airplane or through a tunnel. Offline Support can't help you if the app's data needs to remain up to date with frequent updates or if write requests need to be processed immediately. If you expect your app to be offline for days or weeks at a time, you may also need to find alternative solutions.
Offline Support can be enabled by setting <code>setOffilnePersistency</code> to true on your SDK's client.
</p>

Copy link
Contributor

Choose a reason for hiding this comment

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

For Flutter, it'll be:

client.setOfflinePersistency(status: true);

</p>

<h3>Considerations and Tradeoffs</h3>
[TODO: Code Example]
Copy link
Contributor

Choose a reason for hiding this comment

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

This will be:

client.setOfflineCacheSize(40000); // 40MB

For example, the code example below will <b>block code execution</b> when the device is offline.
</p>

[TODO: Show Example, in comments clearly mark that it's ]
Copy link
Contributor

Choose a reason for hiding this comment

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

Using await like:

    final document = await databases.createDocument(
      databaseId: '[DATABASE_ID]',
      collectionId: '[COLLECTION_ID]',
      documentId: ID.unique(),
      data: {},
    );

will block execution.

Copy link
Contributor

Choose a reason for hiding this comment

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

Updated with more info about state:

  void blockingSubmitTodo() async {
    if (inputController.text.isEmpty || isLoading) return;
    final messenger = ScaffoldMessenger.of(context);
    final newTodo = Todo(
      content: inputController.text,
      id: ID.unique(),
    );

    try {
      await _databases.createDocument(
        databaseId: '[DATABASE_ID]',
        collectionId: '[COLLECTION_ID]',
        documentId: newTodo.id,
        data: {
          "content": newTodo.content,
          "isComplete": newTodo.isComplete,
        },
      );
      inputController.text = '';
      todos.add(newTodo);
    } catch (e) {
      messenger.showSnackBar(createErrorSnackBar(e.toString()));
    }

    setState(() {
      isLoading = false;
    });
  }

</p>

[TODO: Show correct version of example. ]
Copy link
Contributor

Choose a reason for hiding this comment

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

To avoid blocking, don't await

    final document = databases.createDocument(
      databaseId: '[DATABASE_ID]',
      collectionId: '[COLLECTION_ID]',
      documentId: ID.unique(),
      data: {},
    );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stnguyen90 I was really hoping to show an illustrative example where we also show updating local state/ui optimistically to make the point clear.

Is this something we can do?

Copy link
Contributor

@stnguyen90 stnguyen90 Mar 8, 2023

Choose a reason for hiding this comment

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

Updated with more info about state:

  void nonBlockingSubmitTodo() async {
    if (inputController.text.isEmpty || isLoading) return;
    final messenger = ScaffoldMessenger.of(context);
    final newTodo = Todo(
      content: inputController.text,
      id: ID.unique(),
    );

    _databases.createDocument(
      databaseId: '[DATABASE_ID]',
      collectionId: '[COLLECTION_ID]',
      documentId: newTodo.id,
      data: {
        "content": newTodo.content,
        "isComplete": newTodo.isComplete,
      },
    ).catchError((e) {
      messenger.showSnackBar(createErrorSnackBar(e.toString()));
      todos.remove(newTodo);
    });

    setState(() {
      inputController.text = '';
      todos.add(newTodo);
      isLoading = false;
    });
  }

</p>
<p>
[TODO: Describe resolution behavior + tips if available.]
Copy link
Contributor

Choose a reason for hiding this comment

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

When a locally queued request is sent to the server, it will include the timestamp when the request was queued in the X-Appwrite-Timestamp header. Appwrite will compare that request timestamp with the document's $updatedAt timestamp and return a status 409 error if the document's $updatedAt timestamp is newer than the request timestamp. The SDK will revert the changes in the local cache, and then the client can try the request again.

@gewenyu99 gewenyu99 requested a review from stnguyen90 March 8, 2023 19:26
@gewenyu99
Copy link
Contributor Author

@stnguyen90 I was also thinking if there could be a short, self contained code example we can show at the end? might be cool.

@stnguyen90
Copy link
Contributor

stnguyen90 commented Mar 8, 2023

@stnguyen90 I was also thinking if there could be a short, self contained code example we can show at the end? might be cool.

You wouldn't really put everything in 1 self-contained file so I'm not sure if it's good to show that. Maybe we can link to a repo or something once we have the sample app built?

@gewenyu99
Copy link
Contributor Author

@stnguyen90 I was also thinking if there could be a short, self contained code example we can show at the end? might be cool.

You wouldn't really put everything in 1 self-contained file so I'm not sure if it's good to show that. Maybe we can link to a repo or something once we have the sample app built?

Can we add it to the playground app?

:OOOO

@stnguyen90
Copy link
Contributor

Can we add it to the playground app?

@gewenyu99, we plan on updating the todo app to showcase relationships and offline support.

@gewenyu99 gewenyu99 merged commit 6b8ffa0 into feat-offline-support Mar 13, 2023
@gewenyu99 gewenyu99 deleted the feat-offline-support-body branch June 5, 2023 21:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants