可能有两种方法,具体取决于您的服务器端逻辑。
方法1:当服务器不够智能以处理对象状态时。
您可以将所有缓存的记录唯一ID发送到服务器,例如[“ id1”,“ id2”,“ id3”,“ id4”,“ id5”,“ id6”,“ id7”,“ id8”,“ id9”, “ id10”]和一个布尔参数,以了解您是要新记录(拉动刷新)还是旧记录(加载更多)。
您的服务器应负责返回新记录(通过拉动刷新来加载更多记录或新记录)以及从[“ id1”,“ id2”,“ id3”,“ id4”,“ id5”,“ 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,最后一个记录的ID和上一个请求的纪元时间。这样,即使您有大量缓存的记录,您的请求也总是很小
示例:-
如果您请求更多负载,则您的请求应如下所示:-
{
"isRefresh" : false,
"firstId" : "id1",
"lastId" : "id10",
"last_request_time" : 1421748005
}
您的服务器负责返回在last_request_time之后删除的已删除记录的ID,以及在last_request_time之后在“ id1”和“ id10”之间返回更新的记录。
{
"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"]
}
拉动刷新:-
装载更多