【问题标题】:How to obtain x objects with highest timestamp value?如何获取时间戳值最高的 x 个对象?
【发布时间】:2015-06-25 10:07:47
【问题描述】:

我是网络应用程序开发的新手,我找不到以下问题的解决方案。基本上,我正在尝试对 IndexedDB 数据库中的最新对象数组进行排序。每个对象都包含一个时间戳值。我已经在时间戳上创建了一个索引,并且能够获取具有最高值的对象。

    function getMails (numberOfMessages, _function) {
/*  This function uses asynchronous methods, hence you have to pass the key and function that will receive
    the messageitem as a parametr */

    if (typeof optionalArg === 'undefined') {
     optionalArg = 10;
    }
    function _getMails (db) {
        var transaction = db.transaction(["MessageItem"], "readonly");
        var store = transaction.objectStore("MessageItem");

        var index = store.index("timestamp");
        var cursor = index.openCursor(null, 'prev');
        var maxTimestampObject = null;

        cursor.onsuccess = function(e) {
            if (e.target.result) {
                maxTimestampObject = e.target.result.value;
                _function(maxTimestampObject);
            }
        };

    }
    openDB(_getMails);
}

函数 openDB 打开数据库并将 db 对象作为参数传递给 _getMails 函数。函数 getMails 当前仅传递具有最高时间戳值的对象。我可以遍历数据库 x(numberOfMessages) 次并始终选择具有最高时间戳的对象,同时排除我试图获取的数组中已经存在的对象。但我不确定这是否是最方便的方式。谢谢你的回复。一月

【问题讨论】:

  • 你的问题陈述不是很清楚,至少对我来说..你能把它和电话顺序一起安排吗..

标签: javascript indexeddb


【解决方案1】:

您只需要在onsuccess 函数中调用cursor.continue()。它将在下一个游标结果中再次调用。

【讨论】:

    【解决方案2】:

    谢谢觉敦。这是我的最终代码,有兴趣的人可以使用:

    function openDB(_function) {
    // Opening the DB
    var openRequest = indexedDB.open("TsunamiDB",1);
    openRequest.onupgradeneeded = function(e) {
        console.log("Upgrading...");
        var thisDB = e.target.result;
    
        if (!thisDB.objectStoreNames.contains("MessageItem")) {
            var objectStore = thisDB.createObjectStore("MessageItem");
            objectStore.createIndex("timestamp", "envelope.timestamp", {unique:false});
        }
    }
    
    openRequest.onsuccess = function(e) {
        console.log("Success!");
        _function(e.target.result);
    }
    
    openRequest.onerror = function(e) {
        console.log("Error");
        console.dir(e);
    }}
    

    此函数使用异步方法,因此您必须传递将接收消息项作为参数的键和函数。 Parametr 是 x(numberOfMessages) 个最新消息的数组。对数组进行排序,以便索引为 0 的消息是最新消息。

    function getMails ( _function, numberOfMessages) {
    
    if (typeof numberOfMessages === 'undefined') {
     numberOfMessages = 10;
    }
    function _getMails (db) {
        var transaction = db.transaction(["MessageItem"], "readonly");
        var store = transaction.objectStore("MessageItem");
    
        var index = store.index("timestamp");
        var objectsArray = [];
        var i = 0;
    
        index.openCursor(null, 'prev').onsuccess = function(e) {
            var cursor = e.target.result;
            if (cursor && i < numberOfMessages) {
                objectsArray.push(cursor.value)
                ++i;
                cursor.continue();
            }
        };
        transaction.oncomplete = function(e) {
            _function(objectsArray);
        }
    
    }
    openDB(_getMails);}
    

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2014-10-25
      • 2013-08-31
      相关资源
      最近更新 更多