简体   繁体   中英

parse.com trying to get a value from a User class once i have queried another class

i'm trying to post the users location with his display name in the Table View. but all I'm getting in the data browser is the correct post but the display name is blank I'm trying to query the location Class and also the User Class which has the display name for the user

// Configure the new event with information from the location.

CLLocationCoordinate2D coordinate = [location coordinate];
PFGeoPoint *geoPoint = [PFGeoPoint geoPointWithLatitude:coordinate.latitude    longitude:coordinate.longitude];
PFObject *object = [PFObject objectWithClassName:@"Location"];
[object setObject:geoPoint forKey:@"location"];
PFObject *UserObject = [PFObject objectWithClassName:@"_User"];
[[UserObject objectForKey:@"postedUser"] objectForKey:@"displayName"];

// Create relationship
[object setObject:[PFUser currentUser] forKey:@"postedUser"];

[UserObject setObject:[PFUser currentUser] forKey:@"displayName"];


PFACL *locatinACL = [PFACL ACLWithUser:[PFUser currentUser]];
[locatinACL setPublicReadAccess:YES];


[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {

  }
}];

anyone know what I'm doing wrong. the display name is in the user class under displayName and i need it in the new post under displayName

the user save details saved under a form where the set has to fill in extra details after sign up

NSString *saveName = UserNameText.text;
NSString *saveEmail = EmailAddressText.text;
NSString *saveMobile = MobileText.text;
NSString *saveAdd1 = Address1Text.text;
NSString *saveAdd2 = Address2Text.text;
NSString *savePostCode = PostCodeText.text;
NSString *saveCustTax = TaxiOrCust.text;



currentUserSave[@"displayName"] = saveName;
currentUserSave[@"email"] = saveEmail;
currentUserSave[@"mobile"] = saveMobile;
currentUserSave[@"address1"] = saveAdd1;
currentUserSave[@"address2"] = saveAdd2;
currentUserSave[@"postCode"] = savePostCode;
currentUserSave[@"customer"] = saveCustTax;

Forgive the long answer... I think I'm still slightly unclear about your data structure and what you're trying to achieve, so it seems to make the most sense if I clarify what your existing code is actually doing. Hopefully this will help you make any necessary adjustments.

What you're currently doing

PFObject *object = [PFObject objectWithClassName:@"Location"];
[object setObject:geoPoint forKey:@"location"];

Here you're creating a new Location object, and setting the geopoint you've created as its location . If you have an existing Location object you want to update, you'll have to fetch that from Parse (unless you already have a variable / property for it).

PFObject *UserObject = [PFObject objectWithClassName:@"_User"];

Here you're creating a new User object. I suspect you want to use the current user, rather than create a new one, however. There's also a specific Parse object for users: PFUser - which you are using later on.

[[UserObject objectForKey:@"postedUser"] objectForKey:@"displayName"];

This line does nothing. You're fetching UserObject 's postedUser property, and then fetching that user's displayName property. I don't think this is what you were intending.

[object setObject:[PFUser currentUser] forKey:@"postedUser"];
[UserObject setObject:[PFUser currentUser] forKey:@"displayName"];

This then updates the two new objects you created (your location and user objects), by setting two of their attributes to point to the current user.

PFACL *locatinACL = [PFACL ACLWithUser:[PFUser currentUser]];
[locatinACL setPublicReadAccess:YES];

These two lines create a new ACL which gives the current user write access, and everybody else read access. However, you're not applying this to your new location object.

[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
}
}];

Finally, this will save your new Location object. However, the new User object you created will not be saved, as nothing else has a reference to it, and you're not calling save on it.

What you might be trying to do

I think the code you're writing can probably be cut down to something along these lines:

// Create a new location object
PFObject *parseLocation = [PFObject objectWithClassName:@"Location"];

// Create a new PFGeopoint, and store it as the location's location.
CLLocationCoordinate2D coordinate = [location coordinate];
PFGeoPoint *geoPoint = [PFGeoPoint geoPointWithLatitude:coordinate.latitude    longitude:coordinate.longitude];
[parseLocation setObject:geoPoint forKey:@"location"];

// Store the current user and their display name as part of the location object
[parseLocation setObject:[PFUser currentUser] forKey:@"postedUser"];
[parseLocation setObject:[[PFUser currentUser] objectForKey:@"displayName"] forKey:@"displayName"];    

// Create ACL and save it to the location object
PFACL *locationACL = [PFACL ACLWithUser:[PFUser currentUser]];
[locationACL setPublicReadAccess:YES];
[parseLocation setACL:locationACL];

// Save the location object
[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {

  }
}];

Is this closer to what you wanted?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM