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.
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.
I have a parseobject which consists of many objects, notably an array of consisting of ParseUser pointers.
When an individual clicks a button, the array should remove a certain User.
I don't get how to do this,
I have tried:
mRideEdit.removeAll("Participant", (Collection) childuser);
Where mRideEdit is my ParseClass, Participant is the array consisting of ParseUsers, and childuser is the user I want to remove
Please help,
I've recently faced the same problem.
It originates from how the ParseObjects (including ParseUser) are saved on the server.
If you look at the console, you'll see it is actually an array of strings created from the JSONObject toString method.
The simple answer is - You're using it wrong.
There is no sense in saving objects in such a way (though it's very intuitive).
As you probably noticed - you don't receive them as objects with the getJSONArray method either and have to get them from their objectIds.
The best way to do this is using relations.
If you're set on avoiding relations, what I'd suggest is saving them as strings from the get go (objectId). That way add, addUnique and removeAll will work just fine.
Hope this helps.
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.
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'm making (my first) android application and I'm a little bit puzzled with the use of db4o.
I have one activity in which I have a listView, and let the user select an object. Then I pass this object trough a series of intents to other activities (to populate its fields) and then back to the main activity.
But the problem is, when I pass an object (it is serializable), the object I get out of the intent is not the same as the one I put in. (different id, when I check with debug).
All the fields are the same, but it's just not 'the same' object.
So when I try to store the updated object in the db4o, it doesn't recognize it, and stores a double.
I've figured out two workarounds:
Also pass an 'original/unmodified' object, and use it to get the db4o reference (through QBE), and then updating the fields of that object with the values of the changed object.
Using global variables so I don't have to use intents (to pass the object)
But both seem really bad to me? What could be a real solution, instead of a workaround?
You could try using a singleton to store your object and the fields that other classes (?) need to set so everyone has access. I'm not clear of your use of intents in this explanation.