是否可以使用AWS AppSync构建脱机优先的移动应用程序? [英] Is it possible to build offline-first mobile apps using AWS AppSync?

查看:111
本文介绍了是否可以使用AWS AppSync构建脱机优先的移动应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AWS AppSync进行移动开发(Android / iOS),但不确定其离线功能。



根据文档客户端处于脱机状态时可以访问数据,并且如果客户端再次联机将自动同步数据。但是在使用AppSync创建和修改离线数据之前,我找不到有关应用程序客户端是否需要首先连接到AWS的任何信息。



我对AppSync的底层技术(例如GraphQL)不熟悉,并且无法访问公共预览版来对其进行自我测试。



我希望使对隐私敏感的用户无需连接到AWS即可使用应用程序,同时仍然可以将AppSync用作脱机数据库。仅当用户稍后决定跨设备使用备份/同步数据时,他或她才可以选择连接到AWS。



此用例是否会可以使用AWS AppSync吗?



不使用任何其他本地存储(例如SharedPreferences,SQLite,Realm等)

解决方案

Firestore,AWS AppSync或您自己的后端解决方案应该可以实现。无论您使用哪种方法,都可以控制何时开始在线保存/同步内容。



在设计此应用程序时,您需要处理所有这一切。建议的方法



让我们以简单的 ToDo应用为例


  • 我将让用户添加&将Todos保存在应用程序中


  • 所有这些数据都将保留在磁盘上(使用SQLLITE,首选项或文件等)


  • 如果用户确实清除了数据或重新安装了应用程序,则所有这些数据都会丢失

  • 如果用户想获得高级服务,我将让他将该数据与我的后端解决方案同步(任何上述解决方案之一)



示例实施,使用 Android共享首选项作为本地存储

  public void saveLocalTodo(字符串标题,字符串详细信息){
ArrayList< Todo>待办事项
待办事项todo = new Todo(title,details);
字符串listOfTodo = sharedPreference.getString(TODOS_LIST,null);
if(listOfTodo == null)
todos = new ArrayList< Todo>();
else
todos = gson.fromJson(listOfTodo,new TypeToken< ArrayList< Todo>>(){
} .getType());

//保存在第0位,最近应该总是第一个
todos.add(0,todo);
sharedPreference.edit()。putString(TODOS_LIST,gson.toJson(todos))。apply();
}

public ArrayList< Todo> getLocalTodos(){
ArrayList< Todo>待办事项
字符串listOfTodos = sharedPreference.getString(TODOS_LIST,null);
if(listOfTodos == null)
todos = new ArrayList< Todo>();
else
todos = gson.fromJson(listOfTodos,new TypeToken< ArrayList< Todo>>(){
} .getType());
待办事项;
}

public void saveOnBackend(){
//连接到后端解决方案

//从首选项中获取所有本地待办事项
/ /一次全部保存

// OR

//从首选项中获取所有本地待办事项
//一份保存
}


I'd like to use AWS AppSync for mobile development (Android/iOS) but I’m not sure about its offline capabilities.

According to the documentation the data will be accessible while being offline and synced automatically if the client gets online again. But I can't find any information about if the app client needs to connect to AWS first, before using AppSync to create and modify offline data.

I'm not familiar with the underlying technologies of AppSync (e.g. GraphQL) and I don't have access to the public preview version to test it myself.

I would like to enable privacy-sensitive users to use an app without connecting to AWS while still being able to use AppSync as an offline database. Only if a user later decides to use backup/sync data across devices he or she can opt-in to connect to AWS.

Will this use case be possible with AWS AppSync?

Without using any other local storage (like SharedPreferences, SQLite, Realm, etc.)

解决方案

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
}

这篇关于是否可以使用AWS AppSync构建脱机优先的移动应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆