Reputation: 1657
Can I keep some object fields on local storage and not sync them with cloud?
Imagine we have a "Quote" object on network and we want to keep quotes synced between app and cloud. And we have favorite part in our app to keep user favorite quotes separately.
Also we don't want to keep favorites synced with cloud. Can we have property/field like "starred" on local data-store and keep this property/field just in local storage and prevent that from sync with cloud?
Upvotes: 0
Views: 160
Reputation: 11
I have encountered the same problem now while using parse.
In my case I have restaurant catalog app which allows user to bookmark some of the restaurants in order to view them offline. So I want to store a "bookmarked" flag with each restaurant stored locally. Such flag belongs to an individual user and i don't want to sync this flag with parse class. I already have Relation to restaurants in the User class for syncing purposes.
I discovered that if I simply put some data to ParseObject with a key which is not stored in the cloud(ie isn't column of the class) and pin this object locally, such data will be saved localy and will note synced until i intentionally sync restaurant with the cloud. But you can only retrieve such local data with ParseObject when you do query 'fromLocalDatastore()'. Here is my code:
Here I bookmark a restaurant
ParseUser user = ParseUser.getCurrentUser();
restaurant.setBookmarked(doBookmark);
if (doBookmark) {
UserHelper.bookmarksRelation(user).add(restaurant);
restaurant.pinInBackground(ParseRestaurant.BOOKMARKED_PIN);
} else {
UserHelper.bookmarksRelation(user).remove(restaurant);
restaurant.unpinInBackground(ParseRestaurant.BOOKMARKED_PIN);
}
user.saveEventually();
ParseRestaurant :
public class ParseRestaurant extends ParseObject {
...
public static final String LOCAL_KEY_BOOKMARKED = "bookmarked";
...
public Boolean isBookmarked() {
return (containsKey(LOCAL_KEY_BOOKMARKED)) ? getBoolean(LOCAL_KEY_BOOKMARKED) : false;
}
public ParseRestaurant setBookmarked(Boolean bookmarked) {
put(LOCAL_KEY_BOOKMARKED, bookmarked);
return this;
}
...
}
In the result when i query details of the restaurant, its data come with or without "bookmarked" flag, just as i needed:
@Override public Observable<ParseRestaurant> getRestaurant(@NonNull final String restaurantId) {
return Observable.create(new RxUtils.OnSubscribeParse<ParseRestaurant>() {
@Override public ParseRestaurant just() throws ParseException {
ParseRestaurant restaurant = ParseRestaurant.createWithoutData(restaurantId);
try {
restaurant.fetchFromLocalDatastore();
Timber.d("Restaurant is cached locally :)");
return restaurant;
} catch (ParseException unnecessary) {
Timber.d("Restaurant is not cached locally :(");
}
restaurant.fetchIfNeeded();
restaurant.pinInBackground(ParseRestaurant.TEMPORARY_DETAILED_PIN);
return restaurant;
}
}).subscribeOn(Schedulers.io());
}
Upvotes: 1