我有一个存储大量动态数据的 IndexedDB。(静态数据已经被 Service Worker 缓存了)
我的问题是由于这些数据是动态的,我需要清除 IndexedDB 并在每次打开应用程序时将其恢复。为此,我需要增加版本号,以便触发 onupgradeneeded 事件。我想不出任何合乎逻辑的方法来做到这一点,即使在 onupgradeneeded 事件中使用以下调用,我也会得到一个未定义的答案。
e.target.result.oldversion
Run Code Online (Sandbox Code Playgroud)
我的 IndexedDB 代码如下,参数为: Key - 要存储在数据库中的 JSON 对象的名称。值 - JSON 对象本身。
function dbInit(key, value) {
// Open (or create) the database
var open = indexedDB.open("MyDatabase", 1);
console.log(value);
// Create the schema
open.onupgradeneeded = function(e) {
console.log("Old Version: " + e.target.result.oldversion); //Undefined
console.log("New Version: " + e.target.result.newversion); //Undefined
var db = open.result;
var store = db.createObjectStore("Inspections", {keyPath: "id", autoIncrement: true});
var index = store.createIndex(key, key);
};
open.onsuccess = …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,在其中onLongClick将标记放在谷歌地图上。我的问题是在应用程序关闭时保存这些标记。
我使用共享首选项来保存 lat 和 lng 的值,并在应用程序重新启动时加载这些值。但是,当我将这些值传递给一个方法来制作一个标记时,错误猫说我试图在一个空引用上调用一个方法。
任何帮助将不胜感激。请在下面找到代码。
public class SecondMaps extends AppCompatActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener {
private GoogleMap mMap;
EditText addressEditText;
String title;
LatLng position1;
SharedPreferences sharedPreferences;
int locationCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
// Opening the sharedPreferences object
sharedPreferences = getSharedPreferences("location", 0);
// Getting number of locations already stored
locationCount = sharedPreferences.getInt("locationCount", …Run Code Online (Sandbox Code Playgroud)