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.
Related
i have some code like this:
ParseObject firstObject = ParseObject.create("oneClass");
//ParseObject secondObject = already exist on cloude - AnotherClass;
firstObject.put("pointer",secondObject);
firstObject.saveInBackground();
SecondClass beforeSave & afterSave triggered from some reason...
any idea?
When you save an object that has pointers attached to it, those pointers are also saved. This can be useful, or it can cause problems where your saves time out because you're calling extra beforeSave triggers. You just have to be aware of it and plan accordingly.
Also, FYI, when you query for objects that have fields that contain pointers, the pointers that are returned are empty pointers, i.e. they just contain the objectId. If you want to access them, use the include method of your query so that those objects are fetched as well.
The parse android documentation states this:
Objects can have relationships with other objects. To model this behavior, any ParseObject can be used as a value in other ParseObjects. Internally, the Parse framework will store the referred-to object in just one place, to maintain consistency.
What I understand by this is when I do this for put :
firstObject =new ParseObject("A");
secondObject= new ParseObject("B");
secondObject.put("A",firstObject);
According to the last line of the blockquote, this means that the object is not created in B, it simply stores a pointer in B (for A)
Now this createwithoutdata:
You can also link objects using just their objectIds like so:
// Add a relation between the Post with objectId "1zEcyElZ80" and the comment
myComment.put("parent", ParseObject.createWithoutData("Post", "1zEcyElZ80"));
This also means that a pointer is used in place of the object, right?
What is the difference between put and createwithoutdata and what are the usecases where you use each one?
EDIT:
ParseObject parseObject=new ParseObject("TestClass");
ParseObject parseObject1=new ParseObject("TestObject");
parseObject.put("ps1",ParseObject.createWithoutData("TestObject",parseObject1.getObjectId()));
parseObject.saveEventually();
In this each time a new instance is getting created...
You're not asking the right question, actually. The difference between put and createWithoutData is that they're completely different methods with totally unrelated purposes. In both cases you described, you're using the same put method. The difference is in how you're creating the ParseObject to put. In your first example, I believe when you write that to the database, it will insert the B row, then insert the A row with a pointer to the B. In the second example, you are using the ID from a Post row that is already in the database, and writing a pointer to that row into the Comment row.
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.
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!