Accessing PFUser from a NSDictionary - ios

I am attempting to retrieve a PFUser which is inside a NSDictionary however I am not able to retrieve any data when I run my code. The PFUser in the provided dictionary is the User key. Here is my output:
// My dictionary
{
hours = "10 Hours";
location = j;
notes = h;
title = j;
user = "<PFUser: 0x7fea4d578d90, objectId: h1WOITDkrF, localId: (null)> {\n email = \"XXXXX#yahoo.com\";\n fullName = Pradyumn;\n username = Prnk281;\n }";
}
// Should be displaying the PFUser objectId displays null
2015-12-03 22:11:24.774 XXXXX[16176:816409] User ID: (null)
Here is my code as attempt to retrieve the objectId from within the PFUser,
-(void)savePressed:(UIBarButtonItem * __unused)button
{
NSArray * validationErrors = [self formValidationErrors];
if (validationErrors.count > 0){
[self showFormValidationError:[validationErrors firstObject]];
return;
}
NSLog(#"FormValues: %#",self.formValues);
PFUser *selectedUser = [self.formValues objectForKey:#"user"];
NSLog(#"User ID: %#", [selectedUser objectForKey:#"objectId"]);
[self.tableView endEditing:YES];
}

When you make a query to get this data just add one line in the query.
[query includeKey:#"YOUR COLUMN NAME"];
You will get all the details of user in your current dictionary try this out.

I managed to solve my answer by following the two given comments.
First I checked to see if my selectedUser was a available value, which it was:
NSLog(#"Selected user : %#",selectedUser);
Then I did what #paulw11 said and used selectedUser.objectId

Related

objectForKey not working for pointer

In the docs https://www.parse.com/docs/ios/guide#relations-using-pointers
it says that in the example provided you can find the User who created the game
// say we have a Game object
PFObject *game = ...
// getting the user who created the Game
PFUser *createdBy = [game objectForKey:#"createdBy"];
But when I use the exact syntax since I want to populate the pointer in my "user" column
PFUser *user = [PFUser currentUser];
NSString *username = user.username;
// Inside my queryForTable method so PFObject is returned in
// tableView:cellForRowAtIndexPath:object
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"fromUser = %#", user];
PFQuery *query = [PFQuery queryWithClassName:#"Activities" predicate:predicate];
// Inside tableView:cellForRowAtIndexPath:object
PFUser *createdBy = [object objectForKey:#"user"];
NSLog(#"User to user ---%#", createdBy);
But All I get back is
User to user ---<PFUser: 0x7ff9024da860, objectId: aOisP569e3, localId: (null)> {
// Nothing here
}
If I'm understanding correctly, am I also supposed to get back username, email etc in my user object?
---UPDATE 1---
looking at the Anypic app provided by parse it should return something like this
<PFUser: 0x7f7ff9fafc00, objectId: LfADtx1K2J, localId: (null)> {
// Stuff appears here
displayName = "poopiu";
facebookId = 130941340571204;
profilePictureMedium = "<PFFile: 0x7f7ff9faf500>";
profilePictureSmall = "<PFFile: 0x7f7ff9faf7f0>";
username = I34MBM3WYSB5tjWIIvUvhH5fq;
}
but mine is empty even though I have a column called username that isn't undefined so I should get that inside my PFUser object
--UPDATE 2--
Here's what I get back from logging object like so...
NSLog(#"Object---%#", object);
<Activities: 0x7fbbebf42d20, objectId: rDwYI5Inuk, localId: (null)> {
user = "<PFUser: 0x7fbbee1367e0, objectId: SFL0kVZ17x>";
status = 0;
>
Add the following method call after you instantiated your query.
[query includeKey: "user"];
By default, queries do not grab information past the immediate object that was queried.
Are you sure your user isn't nil? According to the docs you must be logged in for currentUser to return a user object.
[createdBy fetch];
NSLog(#"User to user ---%#", createdBy);
Try this hope this will help

Problems with Pointer<Class> in Parse.com

I've been using Parse.com for two months, but this is the first time I get a stupid error which I can't figure out.
I've been working in an app for iOS, but in the beginning I've been working with my own user. Now I registered other users and I want to fetch them from the server.
Well, I add one object in the table Changes, where I have a user property which is a Pointer<User> (it's not _User, but User, it's custom, just an object). Well, when I try to fetch all the rows I have, the one that I have with my user are ok:
so in my debug console is like :
but when I fetch other users:
my debug console is:
so there's not any variable!!!! :(
This is my code:
PFQuery *closestChanges = [PFQuery queryWithClassName:#"Changes"];
[closestChanges whereKey:#"coordinates" nearGeoPoint:geoPoint withinKilometers:0.5f];
[closestChanges findObjectsInBackgroundWithBlock:^(NSArray *changes, NSError *error) {
arrayChanges0 = [[NSMutableArray alloc] init];
arrayChanges1 = [[NSMutableArray alloc] init];
if (changes == nil && changes.count == 0) {
[_tableView reloadData];
return;
}
for (int i = 0; i < changes.count; i++) {
PFObject *currentChange = [changes objectAtIndex:i];
PFObject *user = [currentChange valueForKey:#"user"]; // here my user is null when it's other users.
PFObject *changeToStore = [PFObject objectWithClassName:#"Changes"];
[changeToStore setValue:currentChange[#"changetype"] forKey:#"type"];
[changeToStore setValue:currentChange[#"quantity"] forKey:#"quantity"];
[changeToStore setValue:currentChange[#"date"] forKey:#"enddata"];
[changeToStore setValue:user forKey:#"user"];
if ([currentChange[#"changetype"] intValue] == 0)
[arrayChanges0 addObject:changeToStore];
else
[arrayChanges1 addObject:changeToStore];
}
[_tableView reloadData];
}];
Is there anything wrong I'm doing by adding new users???
Thank you very much in advance.
When you fetch a pointer type from a table, you will only get back the metadata which is objectId. You need to call - (instancetype)includeKey:(NSString *)key in order to get all the data back from Changes table. If you query directly from User table, you will get all the data. But in this case, you User object is a subdocument from Changes objects.
Add this line before performing querying:
[closestChanges includeKey:#"user"]

How to restructure object of NSDictionaries

In my debugger this is how my log statement is printing my messages object:
self.messages = (
"<lean: 0x7fcf98665140, objectId: vglE1UJ5KI, localId: (null)> {\n messageBody = Jj;\n recipientId = XvvxETqjph;\n senderId = XvvxETqjph;\n timestamp = \"1424991590106.210938\";\n}",
"<lean: 0x7fcf98667940, objectId: rgBFYBMKlU, localId: (null)> {\n messageBody = \"test 3 from ian\";\n recipientId = XvvxETqjph;\n senderId = Hoy7UjLzOh;\n timestamp = \"1424631667110.638184\";\n}",
"<lean: 0x7fcf98667f30, objectId: hB5uhwsYsu, localId: (null)> {\n messageBody = \"test 2 from user1\";\n recipientId = XvvxETqjph;\n senderId = VQzxWbDnal;\n timestamp = \"1424630904935.162109\";\n}",
"<lean: 0x7fcf986685b0, objectId: dOe2B9oq5b, localId: (null)> {\n messageBody = \"test 1\";\n recipientId = XvvxETqjph;\n senderId = XvvxETqjph;\n timestamp = \"1424630808309.478027\";\n}"
)
what exactly is this? It looks like an object of dictionaries but the objectId and localId variables are outside of the curly braces so im not sure.
How do I restructure this so that I can have each individual dictionary by itself?
Im getting this data from my backend on parse.com:
-(void)viewWillAppear:(BOOL)animated{
NSString *userId = [[PFUser currentUser] objectId];
PFQuery *query = [PFQuery queryWithClassName:#"lean"];
[query whereKey:#"recipientId" equalTo:userId];
[query orderByDescending:#"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
else {
// We found messages!
masterMessages = (NSDictionary *)objects;
NSLog(#"self.messages = %#", masterMessages);
[self.tableView reloadData];
}
}];
}
this is a simple NSArray which contains objects of type named lean. you can get the needed object by its index
UPDATE:
I can't be sure of course. I have no idea about lean's structure. But most likely it contains field (and I assume it must be a property) named messageBody. If it is really so then you can do the following:
NSMutableArray* messagesBySenderName = [NSMutableArray new];
for (id lean in objects)
{
NSDictionary *messageBody = [lean valueForKeyPath:#"messageBody"];
if ([lean[#"senderId"] isEqualToString:#"Hoy7UjLzOh"])
{
[messagesBySenderName addObject:messageBody];
}
}
Again - it is only an assumption. In no case I can guarantee it will work
They are PFObjects (from the parse.com framework), and PFObject implements description in this particular way. PFObjects implement a dictionary-like interface that is fully described here.
With due respect to others, it is overly general to type them as id, and to use KVO to access their properties. Moreover, messageBody appears to be a column of type string. Treating as a dictionary will be unproductive.
PFObjects should be treated this way:
for (PFObject *pfObject in objects) {
NSString *objectId = [pfObject objectId];
NSDate *createdAt = [pfObject createdAt];
NSString *messageBody = [pfObject objectForKey:#"messageBody"];
NSString *senderId = [pfObject objectForKey:#"senderId"];
// and so on
NSLog(#"This object has id %# created at %#, senderId %#", objectId, createdAt, senderId);
}
I'm aware of your other recent SO question regarding how to derive and array of senderIds from this array. Extracting senderId ought to be obvious from this code, but (a) you probably don't need a new array based on your UI need (a table of conversations), and (b) I think you're actually looking for senderName, and that might mean a substantial change to how you've implemented relationships between parse objects.
I'll try to answer further over on that other question, but this one is clearly related.

How to store the Data from Parse.com to Sqlite DataBase in iOS?

I am using parse.com to get the data like given below. I already created the data base using sqlite manager.
- (IBAction)button:(id)sender {
PFQuery *postQuery = [PFQuery queryWithClassName:#"movie"];
//movie is the class name in the parse.com in that two columns store the data
[postQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
//Save results and update the table
self.postArray = objects;
NSLog(#"array....%#",self.postArray);
// [spinner startAnimating];
[self.myTable reloadData];
}
}];
}
- (void)getTheData:(id)sender {
//it is sqlite query class
model=[[DataModel alloc]init];
//here insertQuery method to send the columns class object(buk).
[model insertInformIntoDB:buk];
PFObject *post = [self.postArray objectAtIndex:indexPath.row];
NSLog(#"posttttt......%#",post);
//here parse.com column name to send the data into sqlite Columns
buk.name=[post objectForKey:#"name"];
buk.Hero=[post objectForKey:#"Hero"];
}
I got the output like this:
//this is overall movie class
array....(
"<movie:EHx3UonJmw:(null)> {\n Hero = Mahesh;\n name = pokiri;\n num = 222;\n}",
"<movie:zekgTzIsLs:(null)> {\n Hero = pawan;\n name = jhoni;\n num = 412;\n}",
"<movie:0z3ZkI4lvB:(null)> {\n Hero = Prabhas;\n name = darling;\n num = 312;\n}"
)
posttttt......<movie:EHx3UonJmw:(null)> {
Hero = Mahesh;
name = pokiri;
num = 222;
}
Error:-
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSMutableString stringWithUTF8String:]: NULL cString'
but here geting null values in buk.name and Hero....
You question is so confusing but still I would love to show what might be wrong,
[model insertInformIntoDB:buk]; should be at the end of the method where you set the properties of the buk variable
-(void)getTheData:(id)sender{
//it is sqlite query class
model=[[DataModel alloc]init];
//here insertQuery method to send the columns class object(buk).
//inserting the buk without setting the values, so move this statement at the end
//[model insertInformIntoDB:buk];
PFObject *post = [self.postArray objectAtIndex:indexPath.row];
NSLog(#"posttttt......%#",post);
//here parse.com column name to send the data into sqlite Columns
buk.name=[post objectForKey:#"name"];
buk.Hero=[post objectForKey:#"Hero"];
//save the buk
[model insertInformIntoDB:buk];
}

Extracting data from a Parse.com class and into an NSArray

I'm using the following query to get some data from a Parse.com class.
What I would like to do is extract the Rating object from the NSArray rateObjects. How would I go about doing this?
thanks for any help
PFQuery *rateQuery = [PFQuery queryWithClassName:#"Rating"];
[rateQuery whereKey:#"photo" equalTo:self.photo];
[rateQuery includeKey:#"photo"];
rateQuery.cachePolicy = kPFCachePolicyNetworkElseCache;
rateQuery.limit = 20;
[rateQuery findObjectsInBackgroundWithBlock:^(NSArray *rateObjects, NSError *error)
{
if( !error )
{
NSLog(#"rateObject %#", rateObjects);
}
}
];
Here's the NSLog output:
rateObject (
"<Rating:w9ENTO29mA:(null)> {\n ACL = \"<PFACL: 0x1e0a5380>\";\n Rating = 4;\n fromUser = \"<PFUser:uV2xu0c3ec>\";\n photo = \"<Photo:Rv4qqrHUPr>\";\n toUser = \"<PFUser:uV2xu0c3ec>\";\n user = \"<PFUser:uV2xu0c3ec>\";\n}",
"<Rating:t3pjtehYR0:(null)> {\n ACL = \"<PFACL: 0x1e0f9f90>\";\n Rating = 5;\n fromUser = \"<PFUser:uV2xu0c3ec>\";\n photo = \"<Photo:Rv4qqrHUPr>\";\n toUser = \"<PFUser:uV2xu0c3ec>\";\n user = \"<PFUser:uV2xu0c3ec>\";\n}"
)
Your NSArray will contain PFObjects, which you can treat in a similar way to a dictionary. In the query you ran above you received two rating objects back. If that's not what you wanted (you only wanted a single object) you may want to revisit how you're querying your data.
Assuming your Rating class in Parse contains a key called Rating you would access it like this:
[rateObject objectForKey:#"Rating"]
You can also use the new modern literal syntax if you like - rateObject[#"rating"]
You'll need to iterate through your array to view all the rating objects that have been returned, so you'll probably end up with something like this:
for (id item in rateObjects) {
int ratingVal = [[item objectForKey:#"Rating"] intValue];
NSLog(#"Rating: %d", ratingVal);
}
You may find Parse's iOS documentation helpful - and if you're not sure what the code above actually does, you may want to review arrays and how they work in Objective-C.
Try to use this:
[rateQuery findObjectsInBackgroundWithBlock:^(NSArray *rateObjects, NSError *error)
{
if( !error )
{
NSMutableArray *data = [[NSMutableArray alloc]init];
[data addObjectsFromArray:rateObjects];
NSArray *rating_data = [data valueForKey:#"Rating"];
NSLog(#"%#",[rating_data objectAtIndex:0]);
}
}
];
I hope this will help you.

Resources