Java – Parsing : How can I get Relation from a local data store (. .. fromLocalDatastore())?

Parsing : How can I get Relation from a local data store (. .. fromLocalDatastore())?… here is a solution to the problem.

Parsing : How can I get Relation from a local data store (. .. fromLocalDatastore())?

I’m developing an application as a learning and I’m using Parse (parse.com) as a data source.

I download all the objects of my class in parsing and save to local storage with parsing. The following code fragment performs one of the 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();
        }
    }

The problem is that I’m downloading all my classes, one is the file type, which is a file used as my news image (“Noticia”). I can download and store all on-site data storage, but I can’t recover with 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;
}

If the “

fromLocalDatastore” query is retracted, he looks for the server and brings in the files correctly, but doesn’t want to track it down again, because as mentioned earlier, the same image is already stored in the local data store.

Another approach is to get the relationship of media IDs and then perform a search to compare ObjectIds in local storage, but I don’t think that’s possible for the property “parent”. But if anything, it can be used as a solution.

Solution

You can’t according to the documentation:

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

There are workarounds, but they may not be practical because, as far as I understand, when you call getFirst(), there is only one image in each ParseObject relation in your “Notica” class. In this case, using Pointer would be a better decision, but they wouldn’t get them by default either, but they would be cached to LocalDatastore。 You need to add the following code to your query to get the Pointer object.

query.include("your_column_key");

Related Problems and Solutions