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.
Related
I have created a PFObject called UserDataTable which stores information like username, password, Name, emailID, age, city, state, etc. of the user.
I am not sure how to authenticate the user to server using PFObject but can do so using PFUsers(). I know that PFUsers is a sub-class of PFObject so there must be a way to access those properties using my PFObject.
Can anyone help me out with the same. I am using SWIFT for coding.
let UserDataBase = PFObject(className: "UserDataBase")
UserDataBase["userId"] = "aamirdbx"
UserDataBase["userName"] = "Aamir Bilal"
UserDataBase["userPassword"] = "pass"
UserDataBase["userEmailId"] = "aamirdbx#gmail.com"
I would like to Sign In using information in this UserDataBase which is a PFObject.
I know how we can do the same using PFUser() but I want to avoid using a bunch of different Objects.
You should be using PFUser for this no matter what. PFUser already has all the built in authentication protocols, session tokens, security and ACL built in for you, which as someone who is just getting started is not something you want to try and manage yourself. You can add extra information to your Parse User class if you need to store additional information, or you can create another table, and have a pointer in your user class to the data in the other table, but for your sake and your user's sake, please don't try to handle this yourself.
After that chunk of code you should call userDataBase.saveInBackground() or saveInBackgroundWithBlock but then you would have to do a lot more coding every time they log in to authenticate and sync objects with the user etc. The included PFUser subclass does a lot of the heavy lifting for you, plus you can add properties to the user subclass so that would probably be a better option.
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.
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.
Is there a way to detect which contact records have changed ?
I understand there are 2 options to detect change:
kABDatabaseChangedNotification and
kABDatabaseChangedExternallyNotification
ABAddressBookRegisterExternalChangeCallback
I would like to detect each contact that has been changed. How would I be able to do this ?
I suppose there is no way to detect the individual changes.
Couple of points to keep in mind:
You need to use `ABAddressBookRegisterExternalChangeCallback1 to
specify the callback function
When the callback function is called reload all the contacts from address book.
Use NSOperation and ensure isCancelled is handled.
When I implement the getLoggedInUserOnSuccess:onFailure method (or the loginWithUsername: password: onSuccess:^(NSDictionary *results)...method in xcode, the results array does not become available until after all of my code has finished running. (If I NSLog the results, they will show up correctly.)
There is one other question that mentions this in Stackoverflow:
How to get the sm_owner User object using StackMob as backend
However, the Stackmob Evangelist in the answer here does not suggest that there is any requirement for a completion block or something of this nature. (And in fact, in his own code, it appears to work without such a block or any sort of "waiting.") This was my first hunch as to what might be going wrong.
(Without posting a ton of code, I am trying to use this function to get the sm_owner which then serves as the predicate in the FetchedResultsController's getter, to ensure the user only sees their own creations and not those of other users, when in one view; in another fetch, they might be able to see the creations of users they follow.)
Has anyone else tried to use one of these methods with a results dictionary returned to write the predicate on a FetchedResultsController or similar and been able to make it work?
None of the Stackmob tutorials appear to limit data returned from a database based on its creator as far as I can tell.
If you set your schema's read permissions to "Allow to sm_owner", then you don't need to place a predicate on the fetch. Doing a generic fetch will return only those objects owned by the current logged in user.