Is it possible to build offline-first mobile apps using AWS AppSync?

◇◆丶佛笑我妖孽 提交于 2019-12-03 00:04:17

It should be possible with Firestore, AWS AppSync or your own Backend solution. Any approach you use, you will control when you want to start saving/syncing things online.

You need to handle all this while designing this app. Suggested approach

Let's take example of simple ToDo app

  • I will let User add & save Todos in app

  • All this data will be persisted on disk(using SQLLITE, Preferences or File etc.)

  • If User does clear data or reinstall app, all this data is lost
  • If User wants to go premium, I will let him sync this data with my Backend solution(any one of above-mentioned solution)

Example implementation using Android Shared preference as local storage

public void saveLocalTodo(String title, String details) {
    ArrayList<Todo> todos;
    Todo todo = new Todo(title, details);
    String listOfTodo = sharedPreference.getString(TODOS_LIST, null);
    if (listOfTodo == null)
        todos = new ArrayList<Todo>();
    else
        todos = gson.fromJson(listOfTodo, new TypeToken<ArrayList<Todo>>() {
        }.getType());

    //save at 0th position, recent should always come first
    todos.add(0, todo);
    sharedPreference.edit().putString(TODOS_LIST, gson.toJson(todos)).apply();
}

public ArrayList<Todo> getLocalTodos() {
    ArrayList<Todo> todos;
    String listOfTodos = sharedPreference.getString(TODOS_LIST, null);
    if (listOfTodos == null)
        todos = new ArrayList<Todo>();
    else
        todos = gson.fromJson(listOfTodos, new TypeToken<ArrayList<Todo>>() {
        }.getType());
    return todos;
}

public void saveOnBackend() {
    // Connect to Backend solution

    // Get all local todos from preference
    // Save all at once in batches

    //OR

    // Get all local todos from preference
    // Save one by one
}

Use Realm Database to manage all offline and online data and save if the application uninstall

you can read https://docs.aws.amazon.com/appsync/latest/devguide/building-a-client-app-reactnative.html

AWS AppSync support offline mode and you can use data base for your app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!