For a new project I was running some tests. For now I have a function in which I am trying to save an object to the parse.com framework and all went well untill I added a user pointer object. The pointer line seems to block my code when I use the current user's objectId as a value for it. Using a random word of the user's email adres however does work. I added my code below.
// Create a data object and store it online.
public void createDataObject(){
ParseObject object = new ParseObject("TestData");
object.put("action", "trial");
object.put("value", "succes?");
object.put("name", ParseUser.getCurrentUser().getUsername());
String pointer = ParseUser.getCurrentUser().getObjectId();
Log.i(TAG, pointer);
object.put("pointer", ParseObject.createWithoutData("_User", pointer));
object.saveEventually();
}
I hope anyone can help me figure it out, help is greatly appreciated!
Update: The problem is not my pointer command but it's the fact I use saveEventually(); If I use saveInBackground it just works but I want to use saveEventually because of possible lost network connection. Does anyone have a clue what could be the problem?
Update 2: Finally decided to delete the app and the installationId of the Parse data browser. Upon reinstalling the app everything started working like it should. I probably had a bad piece of code that got stuck with my installation Id. I hope others with the same problem reach this post fast and don't spend several days searching for an answer!
Update 3: There was a certain function that tried to find a user based on a pointer with the app user's object ID which made the app crash once and than totally unusable. I marked the first answer as the correct answer since it solved my original question but just fyi.. there was more going on than I expected at first.
Just try this:
public void createDataObject(){
ParseObject object = new ParseObject("TestData");
object.put("action", "trial");
object.put("value", "succes?");
object.put("name", ParseUser.getCurrentUser().getUsername());
object.put("pointer", ParseUser.getCurrentUser());
object.saveEventually();
}
No need to get the object ID to set the current User.
Edit:
You can also refer https://stackoverflow.com/a/28403968/2571060 to set pointer
I had a similar issue with the iOS SDK. Are you using 'automatic user creation' ? Cause in that case, ParseUser.getCurrentUser() returns a User object whose objectId is null. According the documentation, it is null until the user or any object related to it is saved. But in my experience, it is not the case, I had to save the user first or I would get an error like 'User can only be created during signUp' or something like this.
Related
I am triying to delete a field of an object in Back4App, but I cannot achieve such a simple operation. By "deleting" I mean set a field that has data to "undefined".
According to the guide, I just have to call myObject.remove("field"). I tryed that (with correct field name), then save the object (I tried all of the saving functions available), but the object is unmodified. There is no error thrown.
I can change the field (with put ("field", otherObject), because it is a pointer field) with no problem. But put("field", JSONObject.NULL) is not working either.
I do not know if this code would work in the original Parse, I am coding this now. The equivalent function in iOS ([myObject removeObjectForKey:#"field"];) in the same database is working nicely...
For what I could gather from your question, the problem is that you're trying to clean a field from a relational object:
"I can change the field (with put ("field", otherObject), because it is
a pointer field)"
On that case, I'm not really sure if using simple object deletion would work. I'd suggest you to take a look at Parse's documentation on Relational Data in order to understand how you should remove that field.
Long story short, I'm not sure if the idea of cleaning the field that you wish will work, but what can be done when you have a relation like this:
ParseUser user = ParseUser.getCurrentUser();
ParseRelation<ParseObject> relation = user.getRelation("field");
relation.add(MyObject);
user.saveInBackground();
Is to simply remove the relation like this:
relation.remove(MyObject);
As you can check in the link above.
My queries with .equalTo() return out-of-date data when used with addListenerForSingleValueEvent, while removing .equalTo() causes the listener to return updated data. Any idea why?
.
I'm using the following query to fetch user's posts from Realtime Database with persistance enabled on Android:
mDatabase.child("posts").orderByChild("uid").equalTo(id)
where id is the id of the current user and each post stores its author's id as a field.
When .equalTo(id) is present, the new posts for the particular user are not returned in that query for the first few minutes. Even more, it seems to affect other queries for the same root ("posts") that contain .orderByChild. Eg, following would also fail to recognise the new post:
mDatabase.child("posts").orderByChild("archived")
Once I remove the .equalTo(id) the behaviour goes back to normal. I'm using addListenerForSingleValueEvent. Tried it also withaddValueEventListener which fires two events, one without the new post, one with it. Without .equalTo(id) both single and non-single listeners return the new post in the first callback. Restarting the app doesn't seem to help straight away - the first event stays out-of-date for the next few minutes. The new post is successfully fetched by different queries in other parts of the application (eg mDatabase.child("posts").child(id))
Any idea why .equalTo() causes such behaviour and how to avoid it (other than using non-single listener and ignoring first event)?
Note 1: same thing happens for .startAt(id).endAt(id)
Note 2: other parts of the Realtime Database are functioning normally, device is connected to the internet and new posts are containing the valid uid field matching the current user.
Update 26/10/2016
Calling mDatabase.child("posts").startAt(key).limitToFirst(4) also produces similar behaviour when trying to query a segment of the database (in our case to implement infinite scroll). It seems that explicitly adding .orderByKey() fixes that particular problem: mDatabase.child("posts").orderByKey().startAt(key).limitToFirst(4).
Though the issue outlined in the original question remains.
I've ran into the exact same problem as you, and after experimenting with almost everything, I've managed to solve it on my end.
I'm scanning barcodes and fetching foods that have the scanned barcode:
Query query = refFoods.orderByChild("barcode").equalTo(barcode);
query.addListenerForSingleValueEvent(new Value ... })};
On my rules i had
".indexOn": "['barcode']"
and after i changed it and took the "[]" out as in:
".indexOn": "barcode"
it started working delay free, where before it would take something like 5 minutes to udpate.
Can't write non current user objects by PFUser currentuser
Referencing the above article, there's countless (literally loads) of iOS examples of how to create a many-many relationship between ParseUsers, and yet not a single one written in Android, there's zero documentation on it whatsoever.
If someone could explain how to do this, that'd be great thank you.
I've tried:
ParseUser friend = getParseUser(username); // I search for a friend here
ParseUser user = ParseUser.getCurrentUser(); // this is me, I have rights to write to me
try {
ParseRelation<ParseUser> relation = user.getRelation("friends");
relation.add(friend);
// if I use user.save(); I get no error back obviously - but it does nothing
// if I use user.saveEventually() I get a neverending loop of ParseErrors
// claiming I can't save to that object - Yes I'm logged in.
user.saveInBackground(new SaveCallback() {
//I report errors further on, but the rest is unreachable as this fails.
And lots of varients of that same code. I don't want to use pointers (Parse DB thing) I want to use their relational model, but this code comes back saying unable to encode an association with an unsaved ParseObject
Again, there's lots of documentation for iOS AND for getting ParseObjects or yourself (ParseUser.getCurrentUser()) but there's nothing that shows how you get a ParseUser (not yourself) then create a relationship with them and yourself. Nowhere.
i have been searching for a while for a solution to my problem without success.
I have an application in which I receive information for a particular entity in my database from different services, so I am using greenDAOs insertOrReplace methods so whenever the entity already exists in my DB it gets updated instead of recreated.
So far so good.
The problem is.. let's say for example sake my entity is called User, with fields id, title, and displayName.
So in the first call I get a JSON object containing a user with only its id and title fields, so I insert it into the DB and naturally the displayName gets inserted as NULL.
Afterwards from another service I get another JSON containing the same user (same id field), but it comes with the displayName as well, but doesn't include the title info at all.
So whenever I run the insertOrReplace on the DAO object automatically generated by greenDAO, the user gets updated but as the title info was not present, when it gets updated the title field gets reset to NULL, so I end up losing data.
Unfortunately I am unable to change the data being returned from the services, and haven't been able to fix this issue. I find it hard to believe there is no easy way to tell the DAO object to update only certain fields and no all of them.
I was looking at the code generated by greenDAO and in the dao objects generated there is a bindValues method which gets called before the query gets executed, and apparently it filters out the NULL properties from the object, but either way it gets updated with the NULL value.
I was able to come up with some sort of fix by modifying the final dao object being generated by adding some methods from the parent class, but I don't think this is a good solution because I would have to do this for all the dao objects. (I know it's possible to define a custom superclass but this only applies for the entity object and not the DAO one).
I would really appreciate if someone has any idea on how I could resolve this, and sorry for the long explanation, I just wanted to be clear on my issue.
Thanks.
First of all: I wouldn't tamper with the generated code unless you really know what you are doing. Modifications may have effects on caches and data-integrity.
Generally you are following this (insert-or)-update-approach if you are using a ORM-Framework (like greendao):
Try to get the entity, that you want to modify from the db (maybe it is already in cache, so this may not be a real database operation)
If you don't have such an entity: create it
Modify the entity according to your needs
Insert or Update it in database (in greendao you would use insertOrReplace)
I am having a weird issue here.
The class Points I want to save data into, has two columns race and challenge which are relations to other classes with the same name.
I have asked this question on Parse.com forums as well
On the client side on Android, if I add the relation via pointsObject.put("race", raceObject), it throws an error saying that the type of "race" is Relation and I am providing a *Pointer
The iOS Counterpart of the app I am working on is completely able to save relations nicely - Happily Coded about 2 hours ago
When I use ParseRelation to add a one-to-many relationship, it takes only the first class as target class.
This code should explain:
ParseRelation<ParseObject> initiatorChallengeRelation = initiatorPoints.getRelation("Points");
initiatorChallengeRelation.add(challenge);
initiatorPoints.put("challenge", initiatorChallengeRelation);
ParseRelation<ParseObject> initiatorRaceRelation = initiatorPoints.getRelation("Points");
initiatorRaceRelation.add(race);
initiatorPoints.put("race", initiatorRaceRelation);
The first block of code sets the target class for the relation as "Challenge".
The second block tries to set the target class of the new ParseRelation object to "Race", but initiatorRaceRelation.add(race) is where it fails and throws an error : "IllegalArgumentException: Related object object must be of class Challenge, but Race was passed in."
I used Eclipse Debugger to check the data in the both Relation objects and found the thing about target classes being the same
Any help on where I might be going wrong?
I have been dealing with this issue since more than 12 hours and its really become a roadblock for me.
Any help would be deeply appreciated
Is Points a relation? From what you have mentioned, it seems so.
Assuming that initiatorPoints is a Points Object, you should get the challenge relation using
ParseRelation<ParseObject> initiatorChallengeRelation = initiatorPoints.getRelation("challenge"); //for challenge
then
initiatorChallengeRelation.add(challenge);
Similarly, for race
ParseRelation<ParseObject> initiatorRaceRelation = initiatorPoints.getRelation("race"); //for race
initiatorRaceRelation.add(race);
I think you need not even use initiatorPoints.put("challenge", initiatorChallengeRelation);
Once, you say
initiatorPoints.saveInBackground(callback), Parse updates the changes in the object by itself.
But do not forget to save the object once you have added the relation data. Hope this helps!