【问题标题】:Parse: How can I get Relation from a local data store (...fromLocalDatastore())?解析:如何从本地数据存储(...fromLocalDatastore())获取关系?
【发布时间】:2015-05-29 14:56:19
【问题描述】:

我正在开发一个用作学习的应用程序,并且我正在使用 Parse (parse.com) 作为数据源。

我正在下载解析中我的类的所有对象并保存到具有解析的本地存储。以下代码 sn-p 执行下载之一:

public void noticia_getOrUpdate(boolean isUpdate) throws ParseException {
        ParseQuery<Noticia> query = new ParseQuery(Noticia.class);
        query.orderByDescending("createdAt");
        List<Noticia> lNoticias = null;

        try {
            if (isUpdate) {
                lNoticias = query.whereGreaterThan("updatedAt", this.sPref.ultimaAtualizacao_noticia()).find();
                if (!lNoticias.isEmpty())
                    ParseObject.pinAllInBackground(lNoticias);
            } else {
                query.whereEqualTo("ativo", true);
                lNoticias = query.find();

                for (Noticia noticia : lNoticias) {
                    if (noticia.getUpdatedAt().getTime() > this.sPref.ultimaAtualizacao_noticia().getTime())
                        this.sPref.atualiza_noticia(noticia.getUpdatedAt());
                }

                ParseObject.pinAllInBackground(lNoticias);
                this.sPref.atualiza_isUpdate(true);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

问题是我正在下载我的所有课程,其中一个是文件类型,是一个用作我的新闻图像的文件(“Noticia”)。我可以下载并存储所有现场数据存储,但无法使用以下代码恢复:

public static byte[] NoticiaMidiaRelation(Noticia noticia) {
        try {
            ParseRelation<Midia> relation = noticia.getImagem();
            Midia midia = relation.getQuery().fromLocalDatastore.whereEqualTo("ativo", true).getFirst();

            if (midia != null && midia.getFileData() != null)
                return midia.getFileData();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    return null;
}

如果撤消“fromLocalDatastore”查询,他会正确地寻找服务器并带上文件,但不想再次寻找它,因为如前所述,本地数据存储中已经存储了相同的图像。

另一种方法是获取媒体 ID 的关系,然后在本地商店中执行比较 ObjectId 的搜索,但我认为没有“父”属性。但如果有的话,可以作为解决方案。

【问题讨论】:

    标签: java android parse-platform local-storage


    【解决方案1】:

    你不能根据文档:

    默认情况下,在获取 obj 时 等等,相关的 ParseObjects 没有被获取

    有一些变通方法,但它们可能不实用,因为据我了解,您的“Notica”类中的每个 ParseObject 的关系中只有一个图像,您称之为 getFirst()。在这种情况下,使用Pointer 会是一个更好的决定,但默认情况下也不会获取它们,但它们会缓存到 LocalDatastore。您需要将以下代码添加到您的 查询 以获取 Pointer 对象。

    query.include("your_column_key");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2017-05-12
      • 2015-11-26
      • 2015-10-18
      • 1970-01-01
      • 2015-02-27
      • 2015-10-22
      相关资源
      最近更新 更多