繁体   English   中英

如何在Parse.com中使用本地数据存储

[英]How to use Local Datastore in Parse.com

我在android中有一个小型项目,在Parse中有数据库。 所以现在我想将我的数据从Parse更改为本地数据存储,我在Parse中启用了本地数据存储,但是我不知道如何更改代码。 这是我从解析代码下载的数据:

try {
        ParseQuery<ParseObject> query = new ParseQuery<>("BUS_STOP");
        query.orderByAscending("stop_id");
        query.setLimit(2000);
        listA = query.find();
        for (ParseObject mBusStop : listA) {
            BusStop newBusStop = new BusStop();
            newBusStop.setName((String) mBusStop.get("name"));
            newBusStop.setStreet((String) mBusStop.get("street"));
            newBusStop.setStyle((String) mBusStop.get("Type"));
            newBusStop.setNext_id((int) mBusStop.get("next_id"));
            newBusStop.setBus_id((int) mBusStop.get("bus_id"));
            newBusStop.setStop_id((int) mBusStop.get("stop_id"));
            double x, y;
            x = (double) mBusStop.get("coor_x");
            y = (double) mBusStop.get("coor_y");
            LatLng a = new LatLng(x, y);
            newBusStop.setLatLngBus(a);
            busStops.add(newBusStop);
        }
    } catch (com.parse.ParseException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }

我的BusStop课程:

@ParseClassName("BUS_STOP")
public class BusStop extends ParseObject{
String name;
String street;
String style;
LatLng latLngBus;
int bus_id;
int next_id;
int stop_id;
public int getStop_id() {
    return stop_id;
}
public void setStop_id(int stop_id) {
    this.stop_id = stop_id;
}

public int getNext_id() {
    return next_id;
}

public void setNext_id(int next_id) {
    this.next_id = next_id;
}

public int getBus_id() {
    return bus_id;
}

public void setBus_id(int bus_id) {
    this.bus_id = bus_id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getStreet() {
    return street;
}

public void setStreet(String street) {
    this.street = street;
}

public String getStyle() {
    return style;
}

public void setStyle(String style) {
    this.style = style;
}

public LatLng getLatLngBus() {
    return latLngBus;
}

public void setLatLngBus(LatLng latLngBus) {
    this.latLngBus = latLngBus;
}

我的申请文件:

public class FindingBusStop extends android.app.Application {
@Override
public void onCreate() {
    super.onCreate();
    Parse.enableLocalDatastore(getApplicationContext());
    ParseObject.registerSubclass(BusStop.class);
}

我该如何解决?

首先,在保存数据时,必须将数据(将ParseObject与本地数据库相关联的术语)“固定”到本地数据库。 因此,例如,如果在按下按钮时保存了数据,则应在setOnClickListener()中添加以下内容

ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);

//使用此行将对象固定到本地数据库并将其保存在那里

gameScore.pinInBackground();

还有另一个函数.pinAll()或pinAllInBackgroud()

然后,在上面的try部分中的代码中,应在find()调用之前添加以下内容。

query.fromLocalDataStore();

您可能还需要使用findinbackground()或getinbackground()函数,这些函数将在后台线程中获取数据。

此处的文档对https://parse.com/docs/android/guide#local-datastore-querying也是有用的。

您可以通过此行query.fromLocalDatastore();从本地数据存储区中获取查询来轻松实现此query.fromLocalDatastore();

我也无法看到您在应用程序类中的何处初始化了解析sdk,如下所示: ... Parse.enableLocalDatastore(getApplicationContext()); Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY"); ... Parse.enableLocalDatastore(getApplicationContext()); Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY");

有一个不错的编码。

暂无
暂无

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

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