I am trying to get subscriptions added to all PFInstallations for a particular PFUser (for cases of one person using same login on iPhone iPad, etc.). I have a table view of all their Facebook friends who use the app as well. On selecting the friend in row, I want it to get my objectId and query all PFInstallations to get an array of all the PFInstallations where the key usersObjectId matches the PFUser objectId. Then, I can add a value to the channels of each of those PFInstallations. I have:
FriendArray *job = self.jobsArray[indexPath.row];
PFQuery *query = [PFUser query];
//job.facebookid is the Facebook id for that particular user
//each PFUser has a fbId and for those who logged in with Facebook, their Facebook ID is stored here
[query whereKey:#"fbId" equalTo:job.facebookid];
PFUser *user = (PFUser *)[query getFirstObject];
//This gives me the PFUser whose fbId value matches the Facebook id for the row that was selected
NSString *subscription = [#"User_" stringByAppendingString:user.objectId];
PFUser *me = [PFUser currentUser];
PFQuery *pushQuery = [PFInstallation query];
//Trying to get all PFInstallations that match the current user's usersObjectId, so I can add the value to each channel
[pushQuery whereKey:#"usersObjectId" equalTo:[me objectId]];
PFInstallation *allInstallations = (PFInstallation *)[pushQuery findObjects];
[allInstallations addUniqueObject:subscription forKey:#"channels"];
It tells me, though, that PFInstallation cannot be directly queried. How can I do the same thing in cloud code?
Ok, after hours of work, I finally figured it out, and thought would post the solution. If you want to edit all your PFInstallation entries of a certain type (I do this by always putting my PFUser objectId as a new value on PFInstallation), here you go.
For cloud code use:
Parse.Cloud.define("subscribingAll", function(request, response) {
Parse.Cloud.useMasterKey();
var usersObjectId = request.params.usersObjectId;
var newSubscription = request.params.newSubscription;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("usersObjectId", usersObjectId);
pushQuery.find({
success: function(results) {
response.success(results);
},
error: function() {
response.error("failed");
}
});
});
Then, on your app, you need a PFCloud call to handle all this.
[PFCloud callFunctionInBackground:#"subscribingAll"
withParameters:#{#"usersObjectId": [me objectId], #"newSubscription": subscription}
block:^(NSArray *theCount, NSError *error) {
if (!error) {
int i;
for (i = 0; i < [theCount count]; i++) {
PFInstallation *change = [theCount objectAtIndex:i];
[change addUniqueObject:subscription forKey:#"channels"];
[change saveInBackground];
}
}
}];
The cloud code returns an array where each object is the data from PFInstallation matching the query. You need to run this through a loop, and set each objectAtIndex as a PFInstallation. From there, just do a simple addUniqueObject, and voila, you are done.
In my case, when logging in, it duplicates the objectId to a key called usersObjectId that I made for PFInstallation. So, I login on my iPhone and then again on my iPad, I have 2 different PFInstallations but both with the same usersObjectId. Running all this code allows me to isolate all of my owned Installations and edit them, specifically to go along with the code I use for subscribing to Facebook friends, so that I can be notified when they post something.
It looks like you can only modify a PFInstallation on the current device, and not all from one user or in the cloud.
This is the code shown on parse :
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation addUniqueObject:#"Giants" forKey:#"channels"];
[currentInstallation saveInBackground];
Maybe a workaround could be adding a new default channel that you subscribe every user to that sends them a notification if they should subscribe to a new channel? I'm unsure if this is the best solution, as I do not know in what context you are performing this subscription.
Related
I'm using Parse SDK, login and signup are working perfectly : [PFUser currentUser] is returning the current user.
But after restarting app, [PFUser currentUser] is returning nil.
Why is the application not persisting the session ?
Login code (from parse.com) I'm using :
[PFUser logInWithUsernameInBackground:self.username.text password:self.password.text
block:^(PFUser *user, NSError *error) {
if (user) {
// Do stuff after successful login.
} else {
// The login failed. Check error to see why.
}
}];
EDIT 2: Create new project, it works. I don't know how and why, but it works.
EDIT: There is no logoutin the whole project
It looks like you are calling .logout somewhere, in which case the app will return nil for [PFUser currentUser].
The current user will persist from session to session if you do not automatically log out the user.
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
//save the installation
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
currentInstallation[#"installationUser"] = [[PFUser currentUser]objectId];
// here we add a column to the installation table and store the current user’s ID
// this way we can target specific users later
// while we’re at it, this is a good place to reset our app’s badge count
// you have to do this locally as well as on the parse server by updating
// the PFInstallation object
}
I am using Parse as my backend for my app. Once the user logs into their account I am trying to get the next view to say "Welcome, (First Name)" at the top. However, I cannot seem to figure out how to accomplish this even using Parse's online documents. Their site directed me here for further assistance. I have tried using their query feature, but could not figure it out. In other words, I am trying to pull the current logged in user's first name, from the database and display it once logged in.
Current code:
PFQuery *query = [PFUser query];
[query whereKey:#"firstName" equalTo:currentUser]; // find user's first name
NSArray *firstName = [query findObjects];
Previous code:
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
// do stuff with the user
Welcome.text = [NSString stringWithFormat:#"Welcome,", currentUser];
Your overcomplicating things.
You don't have to execute a query every time the view loads, instead you should put this in a plist or NSUserDefaults as not to use an API request simply to display the current users name.
However, you can do the following to the current users username :
if ([PFUser currentUser]) {
Welcome.text = [NSString stringWithFormat:#"Welcome, %#", [PFUser currentUser].username];
}
First of all you should check if you actually sign-up and/or logged into Parse with this kind of function:
[PFUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
}];
[PFUser logInWithUsernameInBackground:#"My username" password:#"My password" block:^(PFUser *validUser, NSError *error) {
}];
Check this link: https://parse.com/docs/ios_guide#users-signup/iOS
After you did this, whenever you you want to retrieve your user information,
[PFUser currentUser] is the right way to call some information:
Say for example you want to retrieve the objectId you can get it like this:
NSString *str = [PFUser currentUser].objectId;
Or say you want to set a custom value like this:
NSString *str = #"My custom object";
[PFUser setObject:str forKey:#"MyCustomObject"];
[PFUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
}];
Than you retrieve it like this:
NSString *str = [PFUser objectForKey:#"MyCustomObject"];
You should really check the documentation and example by Parse that are really well explained ! ;)
(Here are some tutorials/Examples by Parse: https://parse.com/tutorials)
How is the transition from your login to your main view set up? Are they both two different controllers?
If so, you should look into NSNotificationCenter...
In your MainViewController, implement
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loadObjects)
name:#"loginFinished"
object:nil];
And loadObjects will be
- (void)loadObjects
{
Welcome.text = [NSString stringWithFormat:#"Welcome %#", [[PFUser currentUser] objectForKey:"userNameField"]];
}
* You need to parse the PFUser object to access its fields. Its just a dictionary so supply it a key 'username' or whatever, and you receive a value 'myusername'. *
Then in your LoginViewController, within your [PFUser logInWithUsernameInBackground:password:block
Implement this
[PFUser logInWithUsernameInBackground:#"My username" password:#"My password" block:^(PFUser *validUser, NSError *error) {
if (!error) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"loginFinished" object:nil];
}
}];
But this is basically how you should setup your login->main flow. Learn NSNotifications, Delegation, and maybe KVO... Learning these will make you a understand how data can be passed around in the iOS/Mac environment.
Heres an analogy for all of them:
NSNotification: A teacher(NSNotification poster) announces a test to all his students(NSNotification observer), or at least the one's who are currently in class, students missing class aren't observing.
Delegate: A student finishes a test and informs the professor(delegate).
KVO: A student(KVO poster) completes a question and raises their hand where the teacher or even other students could be KVOs (key-value observers) and act on their action.
I am trying to update a chat object I have saved with Parse, and although it works sometimes, it's not consistent. If I clear the object out of data on the browser side, it will work a few times, but then I get the error:
Error: object not found for update (Code: 101, Version: 1.3.0)
Here is the code I am using, although I've tried many ways. This code is nearly identical to the Parse documentation.
PFObject *currentChatroom = _currentChatroom;
NSString *objID = currentChatroom.objectId;
PFQuery *query = [PFQuery queryWithClassName:#"Chats"];
// Retrieve the object by id
[query getObjectInBackgroundWithId:objID block:^(PFObject *fetchedChat, NSError *error) {
// Now let's update it with some new data. In this case, only cheatMode and score
// will get sent to the cloud. playerName hasn't changed.
fetchedChat[#"lastTextSent"] = lastTextWithUser;
fetchedChat[#"lastTextSentDate"] = date;
[fetchedChat saveInBackground];
}];
For good measure, Here is the Parse recommendation:
PFQuery *query = [PFQuery queryWithClassName:#"GameScore"];
// Retrieve the object by id
[query getObjectInBackgroundWithId:#"xWMyZ4YEGZ" block:^(PFObject *gameScore, NSError *error) {
// Now let's update it with some new data. In this case, only cheatMode and score
// will get sent to the cloud. playerName hasn't changed.
gameScore[#"cheatMode"] = #YES;
gameScore[#"score"] = #1338;
[gameScore saveInBackground];
}];
The Code works sometimes, so I know that's not the issue. I'm just not sure what is.
The code I used to fix this problem was allowing each user of the object (chatroom in this case) to have ACL Permissions to edit (writeAccess) the PFObject when it was first created. In order to do this, I used the code:
PFObject *newChatroom = [PFObject objectWithClassName:#"Chats"];
// Create ACL to allow both users to edit/update the chatroom
PFACL *multipleUserRights = [PFACL ACL];
// _currentFriend is one user in the chatroom
[multipleUserRights setReadAccess:YES forUser:_currentFriend];
[multipleUserRights setWriteAccess:YES forUser:_currentFriend];
// Give the current user permission as well
[multipleUserRights setReadAccess:YES forUser:[PFUser currentUser]];
[multipleUserRights setWriteAccess:YES forUser:[PFUser currentUser]];
newChatroom.ACL = multipleUserRights;
I found similar questions to this, and some had similar solutions, but not with the error 1.3.0, so I wouldn't consider it a duplicate.
I'm trying to delete the image connected to the current user from the imageOne column in Parse.com. From the user class.
PFQuery *query = [PFUser query];
[query selectKeys:#[#"imageOne"]];
[query getObjectInBackgroundWithId:[[PFUser currentUser] objectId] block:^(PFObject *object, NSError *error) {
if (!error) {
[object deleteInBackground];
}
}];
My code doesn't work and the console logs this error "User cannot be deleted unless they have been authenticated via logIn or signUp".
How can I fix this?
Seems like the problem comes from the fact that object (image) comes from the user class, am I right?
Why are you doing a query for all users and then doing the delete for just the current user, that's the worst possible way to structure the query (and most likely to fail).
If the current user isn't in the first 100 returned your above code would never find a match.
This sort of query should instead be done using getObjectInBackgroundWithId:block:, but in the case of the current user you already have the object, just do this:
[[PFUser currentUser] deleteInBackground];
If instead you just want to delete information in a column, use the following:
PFUser *currentUser = [PFUser currentUser];
[currentUser removeObjectForKey:#"imageOne"];
[currentUser saveInBackground];
I would like write a PFUser object by the currentUser, i've added the ACL based on the Parse developer guide, but i still get an error:
'User cannot be saved unless they have been authenticated via logIn or signUp'
_ My code:
PFQuery *query = [PFUser query];
[query whereKey:#"username" equalTo:self.bBo];
PFObject *friendData = [query getFirstObject];
PFUser *user = (PFUser *)friendData;
PFACL *userACL = [PFACL ACL];
user.ACL = userACL;
[userACL setWriteAccess:YES forUser:[PFUser currentUser]];
PFRelation *friendRelation = [user relationforKey:#"array"];
[friendRelation addObject:[PFUser currentUser]];
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error){
NSLog(#"Error %# %#", error, [error userInfo]);
}
}];
I think i did everything correct, so i can't figure out what can be the problem. So if you made earlier something like this or know where is the problem, i would really appreciate any suggestions.
For security reasons, Parse won't allow you to save any changes to a user that is not currently logged in.
If you want to be able to make and save changes to user, you need to use Cloud Code and the Master Key to get around this roadblock.
I have had multiple problems like this before, and every time I've been forced to use a workaround via Cloud Code.
Here's an example of a workaround I did for creating a friends relationship between two users:
[PFCloud callFunction:#"editUser" withParameters:#{#"userId": user.objectId}];
The above code statement is in xcode, and executes the function I have added to my Cloud Code file.
Then, here's what my Cloud Code file looks like:
Parse.Cloud.define('editUser', function(request, response) {
var userId = request.params.userId;
var User = Parse.Object.extend('_User'),
user = new User({ objectId: userId });
var currentUser = request.user;
var relation = user.relation("friendsRelation");
relation.add(currentUser);
Parse.Cloud.useMasterKey();
user.save().then(function(user) {
response.success(user);
}, function(error) {
response.error(error)
});
});
The above code uses the Master Key to save changes to both the currently logged in user, and to the user who's objectId was passed into this function.
Code details:
var relation
This is just a variable I'm creating to hold what the following fucntion returns:
user.relation("friendsRelation");
In the above function, "friendsRelation" is the name of my PFRelation key in Parse.
Now that we have a valid relation object contain in our variable called relation, we execute this function with an argument of our currentUser object.
After that, all that's left is saving everything. I don't program with javascript, but I was still able to come up with the above solution by looking at the Parse Cloud Code docs, and searching around on their support forums.
If you take my template from above, and make some small changes, then you should be able to easily accomplish what you need. You just have to jump through these extra hoops because the Parse SDK doesn't want a situation where someone can login and somehow make changes to another user's account, whether by their own fault or a developer's mistake.
EDIT:
Here is the code to add the relationship for the current user:
PFRelation *friendsRelation = [[PFUser currentUser]relationforKey:#"friendsRelation"];
PFUser *user = [self.parseUsers objectAtIndex:indexPath.row];
[friendsRelation addObject:user];
[currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(#"%# %#", error, [error userInfo]);
}
}];
And then you can call the Cloud Code method right after:
[PFCloud callFunction:#"editUser" withParameters:#{
#"userId": user.objectId
}];