解析:如何从本地数据存储 (...fromLocalDatastore()) 获取 Relation? [英] Parse: How can I get Relation from a local data store (...fromLocalDatastore())?

查看:23
本文介绍了解析:如何从本地数据存储 (...fromLocalDatastore()) 获取 Relation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing an app to serve as a learning and I'm using Parse (parse.com) as a data source.

我正在解析中下载我的类的所有对象并保存到具有 Parse 的本地存储.以下代码片段执行下载之一:

I am conducting download all objects of my classes in the parse and saving to a local store that has Parse. The following code snippet that performs one of downloads:

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").我可以下载并存储所有现场数据存储,但无法使用以下代码恢复:

The problem is I'm downloading all my classes, one is the File type, is a file that works as an image for my news ("Noticia"). I can download and store all on-site data storage, but can not recover using the following code:

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"查询,他会寻找服务器并正确带入文件,但不想再次追查,因为如上所述,本地数据存储中已经存储了相同的图像.

If retreat the "fromLocalDatastore" the query, he seeks the server and brings the file correctly, but do not want to pursue it again, because as said, already have the same image stored in the local data store.

另一种方法是获取媒体 Id 的关系,然后在本地存储中执行比较 ObjectId 的搜索,但我认为没有办法获取属性父".但如果有的话,可以作为一个解决方案.

Another way to do would be to get the relationship the media Id, after that perform a search for comparing ObjectId within the local store, but I think there's no way the property "parent". But if any, can be used as a solution.

推荐答案

你不能根据文档:

默认情况下,在获取 obj 时ect, 相关的 ParseObjects 不会被获取

By default, when fetching an obj ect, related ParseObjects are not fetched

有一些变通方法,但它们可能不实用,因为据我所知,当您调用 getFirst() 时,Notica"类中的每个 ParseObject 的关系中只有一个图像.在这种情况下,使用 Pointer 将是一个更好的决定,但默认情况下不会获取这些,但它们会缓存到 LocalDatastore.您需要将以下代码添加到您的 query 中以获取 Pointer 对象.

There are work arounds but they might not be practical because by what I understand, you only have one image in your relation per ParseObject in your "Notica" class as you call getFirst(). In this case, using a Pointer would be a better decision but those aren't fetched by default either BUT they are cached to the LocalDatastore. You'll need to add the following code to your query to fetch the Pointer object.

query.include("your_column_key");

这篇关于解析:如何从本地数据存储 (...fromLocalDatastore()) 获取 Relation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆