在我的应用程序中,我需要一个FIFO列表。所有数据都保存在共享首选项中,以确保我始终可以提取所有数据。我按如下方式实现了这个fifo列表:
/**
* This function is used to store a value in the cache and attempt to write it to the server. The writing to the server is done by writing the pushString in the changes buffer. This buffer is then periodically polled and written to the server. This function will also write the changes to the cache, for more details on that see {@link #set(String, JsonObject)}.
* @param key The key to store the value under.
* @param pushString The string to post a network request to to store the data to the server.
* @param jsonObject The value to store.
*/
public void provide(String key, String pushString, JsonObject jsonObject) {
set(key, jsonObject);
int i = 0;
while (changes.retrieve("" + i) != null) {
i++;
}
JsonObject jsonObject2 = new JsonObject();
jsonObject2.addProperty("key", key);
jsonObject2.addProperty("pushString", pushString);
changes.store("" + i, jsonObject2);
changes.save();
}
/**
* The peek method will poll the change-backlog. This will find the first change to be sent to the server. If no changes are to be sent to the server, a null will be returned.
* @return The URL to post a network request to in order to write the changes to the server, null if the backlog is cleared.
*/
public String peek() {
if (changes.retrieve("0") == null || !changes.retrieve("0").get("pushString").isJsonPrimitive()) {
return null;
}
final String res = changes.retrieve("0").get("pushString").getAsString();
return res;
}
/**
* The pop method will pop a message from the change-backlog.
* @return The popped message.
*/
public String pop() {
String toReturn = peek();
int i = 0;
JsonObject nextValue = changes.retrieve("" + (i + 1));
while (nextValue != null) {
changes.store("" + i, nextValue);
i++;
nextValue = changes.retrieve("" + (i + 1));
}
changes.store("" + i, null);
changes.save();
return toReturn;
}
我现在关注的是pop功能。由于相当明显的原因,随着列表大小的增加,此功能往往会越来越长。现在,有大约700个条目,弹出一个条目需要10到20秒。
这引出了两个问题。