根据您的服务器端逻辑,可能有两种方法。
方法 1:当服务器不够智能以处理对象状态时。
您可以将所有缓存的记录唯一 ID 发送到服务器,例如 ["id1","id2","id3","id4","id5","id6","id7","id8","id9", "id10"] 和一个布尔参数,用于了解您是在请求新记录(拉动刷新)还是旧记录(加载更多)。
您的服务器应负责返回新记录(通过拉取刷新加载更多记录或新记录)以及来自 ["id1","id2","id3","id4","id5"," 的已删除记录的 ID id6","id7","id8","id9","id10"]。
示例:-
如果您请求负载更多,那么您的请求应如下所示:-
{
"isRefresh" : false,
"cached" : ["id1","id2","id3","id4","id5","id6","id7","id8","id9","id10"]
}
现在假设您正在请求旧记录(加载更多)并假设“id2”记录已由某人更新,并且“id5”和“id8”记录已从服务器中删除,那么您的服务器响应应如下所示:-
{
"records" : [
{"id" :"id2","more_key":"updated_value"},
{"id" :"id11","more_key":"more_value"},
{"id" :"id12","more_key":"more_value"},
{"id" :"id13","more_key":"more_value"},
{"id" :"id14","more_key":"more_value"},
{"id" :"id15","more_key":"more_value"},
{"id" :"id16","more_key":"more_value"},
{"id" :"id17","more_key":"more_value"},
{"id" :"id18","more_key":"more_value"},
{"id" :"id19","more_key":"more_value"},
{"id" :"id20","more_key":"more_value"}],
"deleted" : ["id5","id8"]
}
但是在这种情况下,如果您有很多本地缓存记录假设为 500,那么您的请求字符串将像这样太长:-
{
"isRefresh" : false,
"cached" : ["id1","id2","id3","id4","id5","id6","id7","id8","id9","id10",………,"id500"]//Too long request
}
方法 2:当服务器足够智能以根据日期处理对象状态时。
您可以发送第一条记录的 id 和最后一条记录以及上一个请求纪元时间。这样,即使您有大量缓存记录,您的请求也总是很小
示例:-
如果您请求负载更多,那么您的请求应如下所示:-
{
"isRefresh" : false,
"firstId" : "id1",
"lastId" : "id10",
"last_request_time" : 1421748005
}
您的服务器负责返回在 last_request_time 之后删除的已删除记录的 id 以及在 "id1" 和 "id10" 之间的 last_request_time 之后返回更新的记录。
{
"records" : [
{"id" :"id2","more_key":"updated_value"},
{"id" :"id11","more_key":"more_value"},
{"id" :"id12","more_key":"more_value"},
{"id" :"id13","more_key":"more_value"},
{"id" :"id14","more_key":"more_value"},
{"id" :"id15","more_key":"more_value"},
{"id" :"id16","more_key":"more_value"},
{"id" :"id17","more_key":"more_value"},
{"id" :"id18","more_key":"more_value"},
{"id" :"id19","more_key":"more_value"},
{"id" :"id20","more_key":"more_value"}],
"deleted" : ["id5","id8"]
}
拉动刷新:-
装载更多