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.
Related
In my app to check for if an email (and username) is taken when signing up I use queries like this...
let emailRef = Firebase(url: "https://photocliq5622144.firebaseio.com/users")
emailRef.queryOrderedByChild("email").queryEqualToValue(email.text!.lowercaseString).observeEventType(.Value, withBlock: { email in
Would this work well with hundreds or even thousands of users (is this scalable)?
Hundreds: yes
Thousands: yes
Tens of thousands... probably (and congratulations on the success of your app)
Hundreds of thousands... you're likely better off with a non query-based data model. For example: if you want to access some data for the user by their email address, store a map from email address to uid:
emailToUid
"HelixProbe#stackoverflow,com": "uid6479958"
"puf#stackoverflow.com": "uid209103"
With such a simple list, you can read the user's data from their email address with two direct lookups, instead of one query (which will get slower as more and more items are added).
Scalable or not is determined by your user.
Behind the scene Firebase library is just downloading JSON string and you know exactly what happen if the string is too long and the file size to be downloaded reach (for example 3MByte)
If your user is able and okay with 3MByte for a request, than you can go with it. But I don't if I am your user.
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 registered a User with PFUser and now i want to add a PfObject for each User something different, so like User A has in the PFObject the number 5 and User B has in the Object the number 8. How can i do that ? I used the PFSignUpViewController sample from Parse
If you are using Objective-c, use this code to create a random number.
int r = arc4random_uniform(74);
Then save the r to the user, the same way you save username and password.
user[#"randomnumber"] = r;
All users have a objectId in parse, so i donĀ“t see the point in doing what you are doing. And most of the information you need can be found in the docs.
https://parse.com/docs/ios/guide#users
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.
I'd like to update user's column which presents related posts that the user might like,
my code is like that:
let users = query.findObjects() as [PFUser]
for user in users{
let rel = user.relationForKey("posts")
rel.addObject(post, forKey: "relatedPosts")
rel.saveInBackground()
}
I really don't know why, but I tried to do that in many versions (not only by relation, also by arrays and other methods) and it always updates just one user (the one who logged in)..
how can I get this done?
You can't update user that is not currently authenticated in your app.
The way I see it you have 2 options:
1) You can set some Cloud Code to run using the master key, so it can modify users.
2) You can add a new custom column to your Parse User class that will link to another class DB that has the related posts for the user.
take a look here: Parse.com Relations Guide
I would choose #2.