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

标签 android database parse-platform local-datastore

我在 Android 中有一个小项目,在 Parse 中有数据库。所以现在我想将我的数据从 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 .

关于android - 如何在 Parse.com 中使用本地数据存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34016682/

相关文章:

android - Android 模拟器中的奇怪消息

android - 联系信标 : Inconsistent and unreliable results when determining beacon distance

android - 如何使用 URL 在两个 Bitbucket 帐户中转移一个项目?

android listview和arrayList数据优化

database - 哪个更好 ?具有更快数据访问的冗余,或者没有冗余和更慢的数据访问

ios - 如何使用项目的价格对 UICollectionView 项目进行排序/排序?

android - apache cordova 应用程序在收到 parse.com 推送通知后崩溃

android - 从 Android 检索 gps 修复中使用的卫星数量

database - 选择、插入、删除时的事务隔离

database - 解析 orQuery 的限制