CouchBase Lite使用setExpirationDate()方法

时间:2018-03-12 17:13:29

标签: android couchbase-lite

我在Android中使用了CouchBase lite 1.4 我想在一段时间后在本地删除文件,我们说15分钟。我一直在阅读,我们可以remove documents locally使用setExpirationDate()方法

所以我正在做的是:

public String createOrUpdateDocument(String jsonDocument) {

        Document d = null;
        String res = null;
        HashMap<String, Object> map = (HashMap<String, Object>) GSONParser.getInstance()
                .fromJson(jsonDocument, HashMap.class);
        if (map.get("_id") != null) {
            //update
            try {
                d = load((String) map.get("_id"));  
            } catch (Exception e) {
            }
        } else {
            d = work.createDocument();
            Date date = Calendar.getInstance().getTime();
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.MINUTE, 15);
            Date expiration = c.getTime();
            d.setExpirationDate(expiration);
        }
        if (d != null) {
            try {
                Revision r = d.putProperties(map);
                map.put("_id", r.getDocument().getId());
                map.put("_rev", r.getDocument().getCurrentRevisionId());
                res = GSONParser.getInstance().toJson(map);
            } catch (CouchbaseLiteException e) {}
        }
        return res;
    }

另一方面,我有一份包含我所有文件的清单,所以我正在做的是为了获得所有文件的清单:

public static List<Object> getWorkDocumentListByTypeAndCompany(String type, Long idCompany){
        if (idCompany == null){
            return getWorkDocumentListByType(type);
        }
        List<Object> l = new ArrayList<Object>();
        View byIdCompany = CouchbaseManager.getInstance().getWorkdocbytypecompany();
        Query q = byIdCompany.createQuery();
        q.setMapOnly(true);
        q.setStartKey(Arrays.asList(type, idCompany));
        q.setEndKey(Arrays.asList(type, idCompany));
        try {
            QueryEnumerator r = q.run();
            for (Iterator<QueryRow> it = r; it.hasNext(); ) {
                QueryRow row = it.next();
                String jsonValue = GSONParser.getInstance().toJson(row.getValue());
                l.add(getFromJSONValue(type, jsonValue));
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return l;
    }

其中

  

.getWorkdocbytypecompany()

是:

 workdocbytypecompany = work.getView("work_doc_bytypecompany");
        workdocbytypecompany.setMap(new Mapper() {
            @Override
            public void map(Map<String, Object> document, Emitter emitter) {
                if (!document.containsKey("ismaster") && document.containsKey("type") && document.containsKey("company")) {
                    List<Object> key = new ArrayList<Object>();
                    HashMap<String, Object> company = (HashMap<String, Object>)document.get("company");
                    Object idcompany = company.get("id");
                    key.add(document.get("type"));
                    key.add(idcompany == null ? null : new Double(idcompany.toString()).longValue());
                    emitter.emit(key, document);
                }
            }
        },"1.2");

但它不起作用,文件没有删除,没有错误,没有信息
我做错了什么?

0 个答案:

没有答案