diff --git a/app/views/docs/offline.phtml b/app/views/docs/offline.phtml index 8fd3a603f..de7a01301 100644 --- a/app/views/docs/offline.phtml +++ b/app/views/docs/offline.phtml @@ -4,17 +4,117 @@

When Should I Enable Offline Support?

- With Offline Support enabled, your app can continue sending requests to Appwrite APIs while offline through a cache. While offline, read requests will be fetched from the local cached data, and write requests will be queued and executed once the device is online again. + 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.

+ +

Enable Offline Support

+

- 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 setOffilnePersistency to true on your SDK's client. +

+ +
+

Adding Offline Support to Existing Projects

+

Enabling Offline Support introduces breaking changes to the behavior of Client SDKs. Asynchronous operations may not resolve until the device is online again. Existing projects will have to be updated.

+

Learn more about asynchronous operations

+
+ +
+
client.setOfflinePersistency(status: true);
+
+

+ Offline Support will cache all read request in a Least Recently Used (LRU) cache, so the information can remain available even with no internet connection. The LRU cache defaults to a size of 16MB and can be configured using the setOfflineCacheSize method. +

+ +
+
client.setOfflineCacheSize(40000);
+
+ +

Asynchronous Operation

+

+ When Offline Support is enabled, asynchronous write operations only resolve after the server accepts the request. Requests made with the Client SDK may block code execution until network connection is available again. +

+[TODO: Show UML diagram] + +

+ For example, the code example below will block code execution when the device is offline.

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

Considerations and Tradeoffs

+ 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; + }); + }
+
+ +

+ To avoid blocking code execution, update local states and UI optimisitically instead of waiting for awaiting asynchronous operations. +

+ +
+
  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;
+    });
+  }
+
+ +

Conflict Resolution

- Offline Support adds a cache layer that takes up additional resources and adds overhead to your requests. This may not be desirable for some applications, especially if it's not expected to be used without a stable internet connection. + With Offline Support enabled, your app can continue sending requests to Appwrite APIs while offline through a cache when offline. Read requests will be fetched from the local cached data, and write requests will be queued and executed once the device is online again. This can lead to write conflicts when the device is online again.

-

Offline Support for Existing Projects

- Asynchronous operations may not resolve immediately when Offline Support is enabled. Existing projects will require some code to be rewritten when adopting Offline Support. Learn more about how to handle asynchronous operation. -

\ No newline at end of file + Document updates made while your device is offline will be queued locally with a timestamp. When your device comes online, Appwrite will compare the timestamps of local updates with the timestamp of the remote document, accepting the latest version. Appwrite will reject a document update if the remote document been updated later than the locally queued update. +

+ +

+ Additional considerations need to be considered when updating data based on the current value, such as incrementing a counter or appending values to then end of a paragraph. The default conflict resolution behavior may result in incorrect updates and writes. Consider disabling these operations while offline or use Appwrite Functions to implement logic to resolve conflicts. +

+ + +