Can I execute multiple Parse Queries in a loop? - android

I'm working on a chat app for Android, and I need to create several different conversations with one message in all of these created conversations. In my code, it looks like that :
for (ParseObject receiver : receivers) {
final ParseObject convQuery = ParseObject.create("Conversation");
convQuery.put("from", fromUser);
convQuery.put("to", receiver);
convQuery.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
ParseObject messageQuery = ParseObject.create("Message");
messageQuery.put("conversation", convQuery);
messageQuery.put("author", fromUser);
messageQuery.saveInBackground();
}
});
}
As you can see, I am executing multiple Parse queries in a for loop. I'm not sure if it's the good way to go... Will it work? Is it optimized? Or is there another better way to do what I want here?
Thanks!

Related

doinbackground method gives null values

i am making an android app in which i am getting the data from the server side by making doinbackground method and assigning these values to the variables which are declared globally. Problem is that i am using that variables in oncreate method where it give me null values because the doinbackground method finishes after the variables are used in oncreate. what could be the solution for this?
public void getProfileInfo() {
String currentUserId = ParseUser.getCurrentUser().getObjectId();
ParseQuery<ParseObject> query = ParseQuery
.getQuery(ParseConstants.KEY_USER_INFO);
query.whereEqualTo(ParseConstants.KEY_USER_ID_INFO, currentUserId);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (ParseObject thisUser : objects) {
name = thisUser
.getString(ParseConstants.TYPE_INFO_FULLNAME);
email = ParseUser.getCurrentUser().getEmail();
}
} else {
Log.d("Activity", "Error: " + e.getMessage());
}
}
});
}
i am calling this method in oncreate before i use the variables name and email.i checked the value of name and email. it gave me the right values.
It sounds like you have a bit of timing problem.
Just to state the obvious, you can't start an asynchronous task on a background thread, and then blindly use the results without knowing that the task has completed.
The use of the results needs to be triggered by the completion of the task.
So, whatever you're doing to access these 'global variables' that is getting nulls - don't do it at a deterministic time (e.g. in onCreate) - do it when you actually get the results back.
If there's a chance that the results may come back before the point where you're currently trying to access them, check to see if they have come back, and if they have, use them. If they haven't, wait until they do come back.
I'll leave the implementation of this as an "exercise for the reader"

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

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.

Parse findInBackground does not work

The find on the db does not complete, so the FindCallback.done() method is never called, no errors, nothing, just is not called. this is the code
ParseQuery<ParseObject> pq = ParseQuery.getQuery("Category").setLimit(1);
pq.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
throw new RuntimeException();
}
});
I can see the prints before and after this portion of code, but the exception is never thrown. Please, I do really need help.
Edit: I use the parse-login before this point and it works correctly, even the saveInBackground() works correctly, the only problems I find are about queries, (find, getfirst ecc).
Resolved, I found out that the background features offered by Parse do not work if in your activity there are fragments, so you have to implement them yourself. The only strange fact is that in the beginning some function (such saveInBackground as I mentioned above) worked.
Check the ACL column in the parse object row, maybe you don't have permission to read.Double click on the ACL column and check the "public Read".
Thy this:
ParseQuery<ParseObject> pq = ParseQuery.getQuery("Category").setLimit(1);
pq.findInBackground(new FindCallback< ParseObject >() {
#Override
public void done(List< ParseObject > parseObjects, ParseException e) {
// Success
} else {
e.printStackTrace();
}
The pq.getFirstInBackground(new GetCallback<ParseObject>() is meant to return only one object, so you need to help it zero in on that object, for example a particular name or item.
Try adding pq.whereEqualTo("item", biscuit);.
If you want to return a list of objects you need to use a different callback.
End product should look like this.
ParseQuery<ParseObject> pq = ParseQuery.getQuery("Category").setLimit(1);
pq.whereEqualTo("item_key_here", "item_value_here");
pq.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
if(object != null){
//do stuff here
}
});

Android Parse SDK issue

I'm using parse to store my data and do a lot of queries while using my program.
The issue is that after about +/-20 similar queries, parse findInBackground() or getFirstInBackground() doesn't return a callback and app stuck at that possition.
My query code:
ParseQuery<OptionCodeDTO> mQuery;
mQuery = ParseQuery.getQuery(OptionCodeDTO.class);
mQuery.whereEqualTo("code", prCode);
mQuery.getFirstInBackground(new GetCallback<OptionCodeDTO>() {
#Override
public void done(OptionCodeDTO optionCodeDTO, ParseException e) {
if (isVisible()) {
if (e == null) {
OptionCode opCode = new OptionCode(optionCodeDTO);
mCodes.push(opCode);
printCodes();
prDescrLayout.setVisibility(View.VISIBLE);
prDescProgress.setVisibility(View.GONE);
mPRLable.setVisibility(View.GONE);
} else {
if (e.getCode() == ParseException.CONNECTION_FAILED) {
mPrDescr.setText(R.string.dtc_lookup_check_network);
} else if (e.getCode() == ParseException.OBJECT_NOT_FOUND) {
mPrDescr.setText(R.string.pr_lookup_code_not_found);
} else {
mPrDescr.setText(R.string.dtc_lookup_other_problems);
}
prDescrLayout.setVisibility(View.VISIBLE);
prDescProgress.setVisibility(View.GONE);
}
}
}
});
First of all, if your app ANRs (application not responding) because of something from UI thread that relies on background threads, that is incorrect architecture.
Probably you have to optimize your app's interaction with Parse. Generally it is a bad practice to make lots of saveInBackground, for example from inside a loop. You can add objects, those need to be saved, to a list and then use ParseObject.saveAllInBackground(objectList)
Also an idea to optimize is to use local storage - android's built in SQLite. For example if your app relies on something being saved to Parse, the logic is like this:
When saving object first you save to local DB and run a saveInBackground method.
When fetching objects you first fetch from your local DB and then run a getInBackground method, which inside a callback persists the information to your local DB.
This way you will make your app usable without internet connection.

CountDownLatch used with two Parse queries?

I was making two Parse.com queries ..
ABC.findInBackground(new FindCallback<ParseObject>()
{
...
}
DEF.findInBackground(new FindCallback<ParseObject>()
{
...
}
and I essentially tried doing this,
CountDownLatch waitForBoth = new CountDownLatch(2);
ABC.findInBackground(new FindCallback<ParseObject>()
{
...
waitForBoth.countDown();
}
DEF.findInBackground(new FindCallback<ParseObject>()
{
...
waitForBoth.countDown();
}
// (error handling (just a flag) not shown)
waitForBoth.await();
Log("both are finished yay!");
of course, I just get the sundry errors IllegalMonitorStateException: object not locked by thread before wait() etc etc etc.
Part of the problem other than me being dense is, those calls to Parse's findInBackground in fact, take you off on a new thread anyway, right?
For the record, one of those Parse calls looks like this,
ParseQuery<ParseObject>PQ = ParseQuery.getQuery("SomeTable");
PQ.whereEqualTo("SomeColumn", ParseUser.getCurrentUser());
PQ.include("SomeOtherColumn");
PQ.findInBackground(new FindCallback<ParseObject>()
{
#Override
public void done(List<ParseObject> res, ParseException e)
{
if (e == null)
{
// res is the List of ParseObject results from the cloud
// here, you'd basically...
waitForBoth.countDown();
}
else
{
int errCodeSimple = e.getCode();
exampleWoeFlag = false;
waitForBoth.countDown();
}
}
});
}
and the doco on that ...
http://parse.com/docs/android/api/com/parse/ParseQuery.html#findInBackground(com.parse.FindCallback)
The Parse SDK does create a new Thread, but the "done" method is running on the UI Thread. This means you are calling "countDown" on the UI Thread, but you will never reach there, because you have called "await" on it.
You could solve it by creating your own Threads and passing the CountDownLatch in it. And then use Parse's synchronous methods
does that make sense ? Someone correct me if Im wrong
By the way, there is an excellent (free) course running on coursera at the moment (https://www.coursera.org/course/posa) that discusses the CountDownLatch in Android.
kind regards
Anton

Categories

Resources