Parse SaveEventually Issue in iOS Swift - ios

I'm facing an issue with save eventually.
I'm retrieving an object from Parse the object contains a pointer for a User.
I'm trying to update the object and save it with a different user.
object.incrementKey("Likes")
object.addObject((PFUser.currentUser()?.objectId)!, forKey: "LikesUsers")
object.saveEventually()
it works once or twice and then it generates this Error and the app crashes :
Caught "NSInternalInconsistencyException" with reason "User cannot be
saved unless they have been authenticated via logIn or signUp
I think the user pointer shouldn't be saved ! and I would like to know if there is any function to tell Parse to not save the User and modify the dirty value.

You should explain what kind of object we're looking at. Is this a "likes" object, or is the "photo" object?
I'm guessing object is a photo or sth that other users kan like. And that what you're trying to do is to save a "like" for the current photo.
Your screenshot is not showing us the LikesUsers column, which is the one you are trying to save to. I will therefore guess that LikesUsers is an array of pointers.
Your code should then be like this:
object.incrementKey("Likes")
object.addObject(PFUser.currentUser()!, forKey: "LikesUsers")
object.saveEventually()
For this to work, the current user must of course be authenticated already.
Be aware that if you have a lot of users, you may face problems if there are thousands of likes on a photo when you store them all in an array of pointers.

Related

Access Parse Object ID right after instantiation

I'm creating two PFObjects at the same time that should reference each other's object IDs when they're saved. In the example below, the second object is supposed to save the first object's object ID in an array.
let objectForFirstClass = PFObject(className:"ClassOne")
let objectForSecondClass = PFObject(className: "ClassTwo")
objectForSecondClass.setObject([objectForFirstClass.objectId!], forKey: "classOneObjectArray")
The last line is causing the error because objectForFirstClass.objectId is nil. I'd assume this is because the object hasn't been saved yet. How can I fix this?
You want to save after creating the first object, and in the completion handler, create the second one with a reference to the first one.
You can use saveAllInBackground:block: for this.
Correct, the object id is assigned by the server when saved. I'd be tempted to write some cloud code to do what you want so you can send some details and the cloud code will create and connect the objects, then return both of them to you. You can of course do the same thing locally in your app, there's just more network comms.
You should also consider using pointers or relationships. These are better for querying, though the same save requirements apply before you can set the connections.

What is the best way to set the followers list using parse.com

so i am working on an app where user can follow each other.. i've been working on it and tried to set the followers in a class Followers and saved the PFUser.currentUser().objectId in the column follower and the objectId of the user in column user i am saving the objectId as Strings. Is this the best possible way or we can use something else to achieve the require things easily. Help me out here please. Thanks for your time..
You should set the column type to Pointer and then have each Pointer be of type User so that you can link directly to the User objects and query on them.

Save a Parse Object Causes Another Object Saved Too

I am quite new with Parse, I am confused on how parse works.
Here I have a PFUser, PFGroupObject, PFUserGroupObject. Where PFUserGroupObject is a "Table" that relate user an group.
PFUserGroupObject and PFGroupObject is my custom PFObject
What confuses me is that, once I save the PFUserGroupObject, the PFGroupObject is also saved to Parse.
I have 2 questions:
Is this behaviour normal? I know that PFUserGroupObject has a variable that stores PFGroupObject. But, I am wondering if this is the expected behaviour of Parse.
//MARK - Create a New Group
var group: PFGroupObject = PFGroupObject();
group.name = "Friends Group";
//MARK - Create a Many to Many Join Table to Relate User and Group
var userGroup: PFUserGroupObject = PFUserGroupObject();
userGroup.user = PFUser.currentUser();
userGroup.group = group;
//THIS LINE CAUSES MY GROUP OBJECT TO BE SAVED TO PARSE AS WELL
PFObject.save(userGroup);
Let's say I called
PFObject.saveAll([group, userGroup]);
Would the group object saved twice into Parse?
I am trying to understand how Parse works.
Thank you!!!!!
When you save a "Parent" object that has one or more "child" objects, it will save the children as well. I have never worked with PFGroupObjects, but I have a pointer to a custom object attached to each of my users. Rather than having to save my custom object, then save my user (or vice versa), saving just my user will also save my custom object.
There are some nuances with this, such as trying to add a pointer to an unsaved object which you have just created to another object, then saving the parent. While you generated an object id when you created the child object, you haven't actually saved it, so it's a dangling pointer.
Hope this helps!
edit - If you want to see exactly what is getting saved when, add a before save trigger to your cloud code for the object you want to see whether or not is being saved multiple times, with a console print line inside. Do some testing, and check your logs to see how many times the beforeSave trigger is getting called, and that's how many times you are attempting to save an object of that type.

Parse CloudCode to remove user from PFRelation

I am new to Parse cloudcode and spinning my wheels to understand JS and write cloudCode to remove user from PFRelation. Can anyone please assist me with parse cloudcode snippet to remove user from PFRelation. I am trying to implement unfriend functionality in the iOS app and I can remove the friend from current users PFRelation and would like to remove current user from the friend PFRelation. I am completely blanked out and don't know how to do that.
I appreciate the help.
Thanks!
The javascript info (that's what you need to do this function on the server) is here: https://parse.com/docs/js_guide#objects-pointers
Specifically, the code on that page that you need is:
var relation = request.user.relation("friends");
relation.remove(other);
request.user.save();
Then you need to get a handle to the other user object and do the same thing there. Are you storing Parse.User objects, or IDs? If it's User objects, the whole code could be this:
var relation = request.user.relation("friends");
var other = relation[0]; // I'm not sure about array indexing though.
var otherRelation = other.relation("friends");
otherRelation.remove(user);
relation.remove(other);
other.save();
request.user.save();
Note that this code isn't really kosher from the perspective of "when the function returns, the logic is done". It's not, since the save is asynchronous. It's faster this way. You could make it both fast and kosher by running both together and waiting for both to finish, but it's been a long time since I've written JavaScript so I can't provide the exact code for that.
Edit: Don't forget to use Parse.Cloud.useMasterKey() to get the right permissions before you try modifying another user.

Core Data Object for ID Only Found Once

I've got a huge xml File which needs to be parsed.
For different Tags inside the xml, e.g Football Soccer Data, I create NSManagedObjects e.g. SoccerPlayer and so forth.
I also need to use these objects a few times within the parsing method and so I created an Object which finds me the right object for the id I provide.
This works fine for the first game inside the xml but won't work for any one after that.
Could be the problem that I have to delete a few objects as I parse through the xml?
For my XML Parsing Framework, I use TouchXML.
Has anyone else experienced this behaviour before?
I agree with the comment that some code would help -- it's hard to understand exactly what the problem is. Nevertheless, I'll point out that the documentation for NSManagedObject's -objectID says:
Important: If the receiver has not yet been saved, the object ID is a
temporary value that will change when
the object is saved.
So, if you're creating an object, storing it's objectID, saving the context, and then trying to find the object with the objectID that you stored, you're probably going to fail because the temporary objectID was replaced with a permanent one when the context was saved.

Resources