Objects still in localstore after unpinAllInBackground(List<...>, DeleteCallback) - android

I'm using Parse.com in my Android app. I'm making a collaborative shopping list which allows the user to mark items for deletion (they turn grey), but they only get actually deleted when I press the Sync button (and there's a network available). Currently, the objects are erased from parse database but not from the local datastore. I'm trying this:
ParseQuery<ShoppingItem> queryDeletes = ShoppingItem.getQuery();
queryDeletes.fromPin(MyApplication.ALL_ITEMS);
queryDeletes.whereEqualTo("isDeleted", true);
queryDeletes.findInBackground(new FindCallback<ShoppingItem>() {
#Override
public void done(final List<ShoppingItem> items, ParseException e) {
if (e == null) {
ShoppingItem.deleteAllInBackground(items, new DeleteCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
ShoppingItem.unpinAllInBackground(items, new DeleteCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
if (!isFinishing()) {
shoppingListAdapter.loadObjects(); // update the list view
}
}
}
});
}
}
});
}
}
});
}
Already tried clearing app data and overriding equals() in ShoppingItem with no success. Any ideas?
Thanks!

Ok, so I solved it. From what I understood, what I was trying to do is not possible using the parse library.
First of all, deleteAllInBackground() also unpins objects, so the unpinAllInBackground() is not needed.
The problem is that I was pinning the objects using item.pin(MyApplication.ALL_ITEMS), thus the only way to unpin them is by passing the pin name using item.unpinInBackground(MyApplication.ALL_ITEMS). However, the batch version does not allow to pass as argument both a collection of items AND the pin name. Thus, it isn't possible to batch unpin items with a named pin.
I ended up unpinning the objects individually passing the pin name. The big complain I have is that doing item.unpinInBackground() without the pin name does not throw an exception and thus I was not aware what the problem was.

Related

Parse Android SDK nested saveInBackground not working

I'm working on an existing Android App with parse back-end (localDatastore is enabled but not used in this context) which has the following object structure:
Object A has an array of objects B
Object B has an array of objects C
I save this object structure using saveInBackground in calling the next saveInBackground in the done SaveCallback in reverse Order(C,B,A). For the inner two that works fine, but the top level object isn't saved.
Here's the code (frame, newStep and order are objects of classes inheriting from the ParseObject class)
frame.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.i("Info", "frame.save callback OK");
frames.add(frame);
newStep.setTimeFrames(frames);
newStep.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.i("Info", "newStep.save callback OK");
List<ProcessingStep> steps = order.getProcessingSteps();
steps.add(newStep);
order.setProcessingSteps(steps);
order.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null){
Log.i("Info", "order.save callback OK");
}else{
Log.i("Info", "order.save callback FAIL");
}
}});
} else {
Log.i("Info", "newStep.save callback FAIL");
e.printStackTrace();
}
}
});
} else {
Log.i("Info", "frame.save callback FAIL");
e.printStackTrace();
}
}
});
In the console log I see only "frame.save callback OK", the "newStep.saveInBackground()" seems to be executed too (object appears in backend) however I never get the log message in the callback.
If I save all objects before synchronously without references to each other first and then call the code here, it seems to work (worked at least once) but took for ever (minutes). Queries from the back-end are super fast and the frame object is also saved almost instantly but the done-callbacks seem to bugging. When it fails I do not get any exception, log anything it just seems to fail silently.
I'm looking for any insight why Parse behaves like that as well as how to fix it.
edit: The problem seems to be with the double relation (A to B and B to C). If I try with only A to B or B to C it works just fine. What remains mysterious to me, however, is why splitting the operation up with callbacks doesn't seem to work.
The problem was the enabled localdatastore. Without localdatastore enabled everything works as it should.

How can I get the list of Open Channels more than 1000 in android using the SendBird SDK?

I'm developing the chatting application using the SendBird SDK in android studio.
How can I get the list of Open Channels more than 1000?
You can create use an OpenChannelListQuery to traverse through your list of channels in fixed chunks.
OpenChannelListQuery query = OpenChannel.createOpenChannelListQuery();
query.setLimit(30);
query.next(new OpenChannelListQuery.OpenChannelListQueryResultHandler() {
#Override
public void onResult(List<OpenChannel> list, SendBirdException e) {
if (e != null) {
// Error!
}
// list contains the first 30 channels.
}
});
As long as your query instance is the same, you can call query.next() until you obtain as many channels as you want.
query.next(new OpenChannelListQuery.OpenChannelListQueryResultHandler() {
#Override
public void onResult(List<OpenChannel> list, SendBirdException e) {
// list contains the next 30 channels.
}
});
Edit: I forgot to mention that the first query.next() must be completely finished before you can call query.next() again. That is, make sure the first onResult() is called before calling query.next() again.

Parse findAllInBackground & fetchAllInBackground

i'm having an issue that soon enough going to blow me.
i have Database table lets call it A. table A has field that determines if this row is processed or no. i update the field myself from within the Parse Browser to either True | False, and trying to call query.findInBackground() to check with the Boolean value however the returned List always returns False if its True and vice versa. enough talking let me show you what i'm doing.
public static void getMyRequests(ParseUser user, final FindCallback<ServicesModel> callback) {
ParseQuery<ServicesModel> query = new ParseQuery<>(ServicesModel.class);
if (!user.getBoolean(ParseHelper.CAN_UPLOAD)) {
query.whereEqualTo("user", user);
}
query.findInBackground(new FindCallback<ServicesModel>() {
#Override public void done(final List<ServicesModel> objects, ParseException e) {
if (e == null) {
if (objects != null && !objects.isEmpty()) {
for (ServicesModel object : objects) {
object.setHandlerUser(object.getParseUser("handlerUser"));
object.setProcessedTime(object.getLong("processedTime"));
object.setCategoryType(object.getString("categoryType"));
object.setUser(object.getParseUser("user"));
object.setUserRequest(object.getString("userRequest"));
object.setImageUrl(object.getString("imageUrl"));
object.setProcessed(object.getBoolean("isProcessed"));
Logger.e(object.getBoolean("isProcessed") + "");
}
callback.done(objects, null);
} else {
callback.done(null, new ParseException(1001, "No Services"));
}
} else {
callback.done(null, e);
}
}
});
}
the code above suppose to refresh my data but however my log always shows that isProcessed is False even tho it's set to True inside the Parse Browser
what i have tried besides this? fetchAllInBackground & fetch() you name it. the object will always return false until i re-run the application from Android Studio what i'm doing here wrong? btw here is how i initialize Parse
Parse.setLogLevel(BuildConfig.DEBUG ? DEBUG_LEVEL : Parse.LOG_LEVEL_NONE);
ParseObject.registerSubclass(ProductsModel.class);
ParseObject.registerSubclass(ProductRentalModel.class);
ParseObject.registerSubclass(ServicesModel.class);
Parse.enableLocalDatastore(context);
Parse.initialize(context, context.getString(R.string.app_id), context.getString(R.string.client_id));
the answer was to remove
Parse.enableLocalDatastore(context);
which is bad anyway, without the datastore enabled the data are refreshed probably, however with enabling the local database, the data will not refresh unless if i killed the app and/or re-install it. that's bad. but did the trick.

Pinning live items (subclass ParseObject) and immediately retrieving via fromPin(pinName) - the list is empty

I have a very simple fragment that basically calls a method that tries to retrieve custom Parse objects from the internet, pin them and then reload a UI ListView with the pinned items whether or not the internet call succeeded (so there is a fallback caching mechanism in case there is no internet connection).
Here are the two key methods:
// This is called in onViewCreated() and onResume() of the fragment
private void reloadWalletsFromInternet() {
ParseQuery<Wallet> queryLiveData = ParseQuery.getQuery(Wallet.class);
queryLiveData.findInBackground(new FindCallback<Wallet>() {
#Override
public void done(final List<com.hasmobi.money.models.Wallet> list, ParseException e) {
if (list != null && list.size() > 0) {
for (final Wallet w : list) {
w.pinInBackground("wallets", new SaveCallback() {
#Override
public void done(ParseException e) {
reloadWalletsFromLocalstore();
}
});
}
} else {
Log.d(getClass().getSimpleName(), "no wallets retrieved from internet");
reloadWalletsFromLocalstore();
}
}
});
}
private void reloadWalletsFromLocalstore() {
final ParseQuery<Wallet> queryLocalData = ParseQuery.getQuery(Wallet.class);
queryLocalData.fromLocalDatastore();
queryLocalData.fromPin("wallets");
queryLocalData.findInBackground(new FindCallback<Wallet>() {
#Override
public void done(List<Wallet> list, ParseException e) {
// Here I am receiving 0 items in "list" which is wrong
// (e is "null")
}
});
}
Inside App.java (my custom Application base class) I've subclassed the Wallet class to be registered in Parse:
ParseObject.registerSubclass(Wallet.class);
Parse.enableLocalDatastore(this);
Parse.initialize(this, "my keys", "my keys");
I've put a couple of strategic Log.d() lines on a few places and the code successfully runs through both methods and the for() loop inside reloadWalletsFromInternet() successfully runs and appears to pin each of the received Wallet objects in the pin group "wallets". However, the subsequent query for the pins in that group, made by reloadWalletsFromLocalstore() don't seem to be able to retrieve those Wallet objects.
Below code will do the job.
get rid of queryLocalData.fromPin("wallets");
private void reloadWalletsFromLocalstore() {
final ParseQuery<Wallet> queryLocalData = ParseQuery.getQuery(Wallet.class);
queryLocalData.fromLocalDatastore();
queryLocalData.findInBackground(new FindCallback<Wallet>() {
#Override
public void done(List<Wallet> list, ParseException e) {
// Here I am receiving 0 items in "list" which is wrong
// (e is "null")
}
});
}

clearCachedResult() not working as expected

I have this query located in my ParseQueryBuilder object:
public ParseQuery<Event> eventsTypes() {
ParseQuery<Event> query = Event.getQuery();
query.setCachePolicy(ParseQuery.CachePolicy.CACHE_ELSE_NETWORK);
query.setMaxCacheAge(TimeUnit.DAYS.toMillis(1));
query.whereEqualTo(Event.owner, parse.getParseUser());
query.orderByDescending(Event.timesUsed);
return query;
}
I use it to populate a ParseQueryAdapter
and at some point I would like to add an Event and immediately show it:
#OnClick(R.id.add)
public void add(Button button) {
final Event new_type = new Event();
new_type.setOwner(parse.getParseUser());
new_type.setName("atest");
new_type.saveEventually(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
// on successfull save, clear cache
parseQueryBuilder.eventsTypes().clearCachedResult();
// and show newly added object
mAdapter.loadObjects();
Toast.makeText(getActivity(), new_type.getName(), Toast.LENGTH_SHORT).show();
}
}
});
}
I expected clearing the cache would result in a new network query, revealing the newly added item but no matter what I try, it seems it will only show the initially cached result.
Even if I try to restart my app, it shows the result from the first cache.

Categories

Resources