简体   繁体   English

解析中的objectId始终为NULL

[英]objectId in Parse is always NULL

In my Activity I init the Parse service : 在我的活动中,我启动了Parse服务

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);

I have this class 我有这堂课

@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);
    }
}

First I want to retrieve data from the Parse service like this 首先,我想像这样从Parse服务检索数据

    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
            }
        }
    });

but the ParseObjects do not have an objectId . 但是ParseObjects没有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);
    }
});

That always throws a ParseException : 总是抛出一个ParseException

com.parse.ParseException: java.lang.IllegalStateException: ParseObject has no data for 'objectId'. com.parse.ParseException:java.lang.IllegalStateException:ParseObject没有“ objectId”的数据。 Call fetchIfNeeded() to get the data. 调用fetchIfNeeded()获取数据。

Well because the ParseObjects actually do not have an objectId . 好吧,因为ParseObjects实际上没有objectId Neither in my local datastore nor when querying directly from Parse. 既不在我的本地数据存储中,也不在直接从Parse查询时。

What is going wrong here? 这是怎么了? I took the code from various examples and it should work. 我从各种示例中获取了代码,它应该可以正常工作。

The problem is getString("objectId"); 问题是getString("objectId"); . You must not implement default Parse Column getters/setters . 您不得实现默认的“解析列”获取器getters/setters

You can get it with getObjectId() which has already been implemented in ParseObject. 您可以使用已经在ParseObject中实现的getObjectId()来获取它。 Also to get updatedAt , createdAt and some other default Parse column, you have to use their own Getters such as getUpdatedAt() and getCreatedAt() . 也让updatedAtcreatedAt和其他一些默认的解析列,你必须用自己的Getters ,如getUpdatedAt()getCreatedAt()

At the below, I removed your objectId getter implementation. 在下面,我删除了您的objectId getter实现。 You should implement only your own column fields. 您应该只实现自己的列字段。

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

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

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

Update 更新

You have included a getter for objectId in your subclass. 您已在子类中包含objectId的获取器。 This is already accessible from the parent class and should not overridden. 这已经可以从父类访问,并且不应覆盖。

Old answer 旧答案

The problem is probably that objects in local datastore does not have an objectId until it has been saved remotely to Parse. 问题可能是本地数据存储区中的对象只有在将其远程保存到Parse之后具有objectId。 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. 在固定的情况下,您不能访问objectId,除非它也已远程保存。

删除您的空构造函数或在其中调用super()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM