Parse 中的 objectId 始终为 NULL [英] objectId in Parse is always NULL

查看:82
本文介绍了Parse 中的 objectId 始终为 NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动中,我初始化了解析服务:

In my Activity I init the Parse service:

Parse.enableLocalDatastore(getApplicationContext());

ParseObject.registerSubclass(Person.class);
ParseObject.registerSubclass(Area.class);

Parse.initialize(this, "MY_APP_ID", "MY_CLIENT_KEY");
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);

我有这门课

@ParseClassName("Area")
public class Area extends ParseObject {    
    public static ParseQuery<Area> getQuery() {
        return ParseQuery.getQuery(Area.class);
    }

    public String getObjectId() {
        return getString("objectId");
    }

    public String getName() {
        return getString("name");
    }

    public void setName(String name) {
        put("name", name);
    }
}

首先我想像这样从 Parse 服务中检索数据

First I want to retrieve data from the Parse service like this

    ParseQuery<Area> areaQuery = Area.getQuery();
    areaQuery.findInBackground(new FindCallback<Area>() {
        public void done(List<Area> areas, ParseException e) {
            if (e == null) {
                ParseObject.pinAllInBackground(areas,
                        new SaveCallback() {
                            public void done(ParseException e) {
                                //output errors
                            }
                        });
            } else {
                //output errors
            }
        }
    });

但 ParseObjects 没有 objectId.
后来我想像这样从我的本地数据存储中检索数据

but the ParseObjects do not have an objectId.
Later I want to retrieve data from my local datastore like this

ParseQuery<Area> query = Area.getQuery();
query.orderByAscending("name");
query.fromLocalDatastore();
query.findInBackground(new FindCallback<Area>() {
    public void done(List<Area> areaList, ParseException e) {
        if (e != null) {
            Log.d("area", "Error retrieving areas");
            return;
        }
        for (Area a : areaList)
            areaResults.add(a);
    }
});

总是抛出一个ParseException:

com.parse.ParseException:java.lang.IllegalStateException:ParseObject 没有objectId"的数据.调用 fetchIfNeeded() 获取数据.

com.parse.ParseException: java.lang.IllegalStateException: ParseObject has no data for 'objectId'. Call fetchIfNeeded() to get the data.

好吧,因为 ParseObjects 实际上没有 objectId.既不在我的本地数据存储中,也不在直接从 Parse 查询时.

Well because the ParseObjects actually do not have an objectId. Neither in my local datastore nor when querying directly from Parse.

这里出了什么问题?我从各种示例中获取了代码,它应该可以工作.

What is going wrong here? I took the code from various examples and it should work.

推荐答案

更新

您在子类中包含了 objectId 的 getter.这已经可以从父类访问,不应覆盖.

You have included a getter for objectId in your subclass. This is already accessible from the parent class and should not overridden.

旧答案

问题可能是本地数据存储中的对象没有 objectId 直到它已被远程保存到 Parse.只有这样它才会得到一个objectId.

The problem is probably that objects in local datastore does not have an objectId until it has been saved remotely to Parse. Only then does it get an objectId.

您需要确保您的对象已远程保存,或者您需要在查询对象之前在本地固定对象.在固定的情况下,您无法访问 objectId,除非它也已远程保存.

You need to make sure your objects have been saved remotely OR you need to pin objects locally before querying them. In the case of pinning, you cannot access the objectId unless it has also been saved remotely.

这篇关于Parse 中的 objectId 始终为 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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