Android SyncAdapter机制

时间:2014-11-07 05:07:54

标签: php android sqlite android-syncadapter

您能否向我解释如何实现同步到我的应用程序的逻辑? 我的意思是,我有同步代码,我有服务,提供商,一切,但我不明白(我有Android编程(OReilly)教程的同步代码)。

所以,我想,同步应该是这样的:

  1. 用户启动应用程序,并指向登录页面,输入用户和密码,然后单击“登录”。
  2. 现在,我的登录方法向我的服务器端发送HTTP请求(远程mySQL + PHP),在服务器端,PHP文件返回JSON中的令牌(如果用户+通行证存在)。
  3. 现在我在应用程序中有令牌,我创建了一个帐户(所以我可以使用Android的同步功能)
  4. 然后,如果登录成功,我启动mainActivity并继续使用应用程序的常规作业。
  5. 现在...
  6. ......到目前为止,非常好。我的帐户显示在“帐户/同步”

    我假设我应该将令牌存储在服务器端(也许在“users”远程表中) 然后我想,我应该实现我的同步逻辑。现在这是我的问题: 同步逻辑应该是这样的吗?

    远程table1结构: 的ID,姓名,出生日期,缺失

    远程table2结构: id,invoice_no,invoice_date,invoice_total,已删除

    本地(SQLite)tablex1结构: _id,姓名,出生日期,缺失

    本地(SQLite)tablex2结构: _id,invoice_no,invoice_date,invoice_total,已删除

    • 我应该在本地表中添加一个像“remote_id”(整数)这样的新字段吗?

    • 然后在远程表中添加一个名为“synced”(boolean)的新字段,或者可能是“synced-timestamp”?

    • 然后有两种方法: pull() (用于从远程获取数据) push() (用于将数据发送到远程)

    • onPerformSync我应该进行如下验证:

    (推送)

          - send to remote a list with all records from tablex1 and tablex2 that have "remote_id" = null
    
          - on the server side, the PHP (after checking the token to be valid) will insert the new rows, with "synced" set to true
    

    (拉)

          - ask the remote to send back to me the list of all records that have the synced=false, then set them to true on remote.
    
          - process the returned rows and insert them locally in SQLite (including "remote_id")
    

    这是一个好方法吗?是否容易出现重复或其他问题?我应该用什么作为逻辑呢?

    你做什么同步?

    你推荐什么逻辑?

    谢谢

0 个答案:

没有答案