diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2015-01-07 09:21:28 +0100 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2015-01-07 09:41:43 +0100 |
commit | 3085cb1bf05fc617a3225edd142b31d48314bff5 (patch) | |
tree | 1160d869c1a762a6d064366e6133ee1929bfbddb /main | |
parent | fa306fb7aefb8385b504cd31d5ef5a7a370df669 (diff) | |
download | cgeo-3085cb1bf05fc617a3225edd142b31d48314bff5.zip cgeo-3085cb1bf05fc617a3225edd142b31d48314bff5.tar.gz cgeo-3085cb1bf05fc617a3225edd142b31d48314bff5.tar.bz2 |
Do not lazy load fields if the cache doesn't come from the database
When a cache is stored in the database, some fields are lazily loaded
from the database. This should only happen when the cache itself has
been loaded from the database, otherwise we might fill fields of a cache
acquired from the network with older version of the fields as stored in
the database.
Noticed during the work on #4576.
Diffstat (limited to 'main')
-rw-r--r-- | main/src/cgeo/geocaching/DataStore.java | 2 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/Geocache.java | 46 |
2 files changed, 31 insertions, 17 deletions
diff --git a/main/src/cgeo/geocaching/DataStore.java b/main/src/cgeo/geocaching/DataStore.java index d36a9c9..f614ab1 100644 --- a/main/src/cgeo/geocaching/DataStore.java +++ b/main/src/cgeo/geocaching/DataStore.java @@ -2946,7 +2946,7 @@ public class DataStore { } public static void saveChangedCache(final Geocache cache) { - DataStore.saveCache(cache, cache.getStorageLocation().contains(StorageLocation.DATABASE) ? LoadFlags.SAVE_ALL : EnumSet.of(SaveFlag.CACHE)); + DataStore.saveCache(cache, cache.inDatabase() ? LoadFlags.SAVE_ALL : EnumSet.of(SaveFlag.CACHE)); } private static enum PreparedStatement { diff --git a/main/src/cgeo/geocaching/Geocache.java b/main/src/cgeo/geocaching/Geocache.java index c842a7f..dda19c8 100644 --- a/main/src/cgeo/geocaching/Geocache.java +++ b/main/src/cgeo/geocaching/Geocache.java @@ -133,13 +133,13 @@ public class Geocache implements IWaypoint { private final LazyInitializedList<String> attributes = new LazyInitializedList<String>() { @Override public List<String> call() { - return DataStore.loadAttributes(geocode); + return inDatabase() ? DataStore.loadAttributes(geocode) : new LinkedList<String>(); } }; private final LazyInitializedList<Waypoint> waypoints = new LazyInitializedList<Waypoint>() { @Override public List<Waypoint> call() { - return DataStore.loadWaypoints(geocode); + return inDatabase() ? DataStore.loadWaypoints(geocode) : new LinkedList<Waypoint>(); } }; private List<Image> spoilers = null; @@ -629,18 +629,25 @@ public class Geocache implements IWaypoint { */ private void initializeCacheTexts() { if (description == null || shortdesc == null || hint == null || location == null) { - final Geocache partial = DataStore.loadCacheTexts(this.getGeocode()); - if (description == null) { - setDescription(partial.getDescription()); - } - if (shortdesc == null) { - setShortDescription(partial.getShortDescription()); - } - if (hint == null) { - setHint(partial.getHint()); - } - if (location == null) { - setLocation(partial.getLocation()); + if (inDatabase()) { + final Geocache partial = DataStore.loadCacheTexts(this.getGeocode()); + if (description == null) { + setDescription(partial.getDescription()); + } + if (shortdesc == null) { + setShortDescription(partial.getShortDescription()); + } + if (hint == null) { + setHint(partial.getHint()); + } + if (location == null) { + setLocation(partial.getLocation()); + } + } else { + description = StringUtils.defaultString(description); + shortdesc = StringUtils.defaultString(shortdesc); + hint = StringUtils.defaultString(hint); + location = StringUtils.defaultString(location); } } } @@ -1013,7 +1020,7 @@ public class Geocache implements IWaypoint { */ @NonNull public List<LogEntry> getLogs() { - return DataStore.loadLogs(geocode); + return inDatabase() ? DataStore.loadLogs(geocode) : Collections.<LogEntry>emptyList(); } /** @@ -1182,6 +1189,13 @@ public class Geocache implements IWaypoint { } /** + * Check if this cache instance comes from or has been stored into the database. + */ + public boolean inDatabase() { + return storageLocation.contains(StorageLocation.DATABASE); + } + + /** * @param waypoint * Waypoint to add to the cache * @param saveToDatabase @@ -1748,7 +1762,7 @@ public class Geocache implements IWaypoint { */ public int getFindsCount() { if (getLogCounts().isEmpty()) { - setLogCounts(DataStore.loadLogCounts(getGeocode())); + setLogCounts(inDatabase() ? DataStore.loadLogCounts(getGeocode()) : Collections.<LogType, Integer>emptyMap()); } final Integer logged = getLogCounts().get(LogType.FOUND_IT); if (logged != null) { |