简体   繁体   中英

How to use Local Datastore in Parse.com

I have small project in android with database in Parse. So now i wanna change my data from Parse to local datastore , i enabled local datastore in Parse but i don't know how to change the code. This is my download data from Parse code:

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

My BusStop class :

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

My Application file :

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

How can i fix it?

Firstly you must 'pin' the data (the term for associating a ParseObject with the local database) to the local database when you save it. So , for example , if data is saved on pressing a button you should add the following in the setOnClickListener()

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

//The use this line to pin the Object to the local DB and save it there

gameScore.pinInBackground();

There is also another function .pinAll() or pinAllInBackgroud()

Then in your code above in the try section you should add the following before your find() call.

query.fromLocalDataStore();

You may also want to use the findinbackground() or getinbackground() function which will fetch the data in a background thread.

The documentation here is also helpful https://parse.com/docs/android/guide#local-datastore-querying .

you can do this easily by fetching your query from local datastore by this line query.fromLocalDatastore(); .

Also i am not able to see where you have initialised parse sdk in your application class like this : ... Parse.enableLocalDatastore(getApplicationContext()); Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY"); ... Parse.enableLocalDatastore(getApplicationContext()); Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY");

Have a nice coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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