I'm trying to get data from a current user's "addedRelation" but the following code isn't working. I get a warning in the console that says PFRelation is readonly as well.
PFQuery *query = [PFQuery queryWithClassName:#"Friends"];
[query whereKey:#"user" containsString:[[PFUser currentUser] objectForKey:#"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for (Friends *currentFriend in objects) {
self.relation = currentFriend.friendsRelation;
self.addedRelation = currentFriend.addedRelation;
self.query = [_relation query];
self.array = [_query findObjects];
self.array2 = [NSMutableArray arrayWithArray:self.array];
// self.addedRelation = [[PFUser currentUser] relationForKey:#"addedRelation"];
self.addedQuery = [_relation query];
self.addedArray = [_query findObjects];
self.addedArray2 = [NSMutableArray arrayWithArray:self.addedArray];
[self.segmentControl setTitle:[NSString stringWithFormat:#"Friends (%lu)", (unsigned long)[self.array2 count]] forSegmentAtIndex:0];
[self.segmentControl setTitle:[NSString stringWithFormat:#"Added Me (%lu)", (unsigned long)[self.addedArray2 count]] forSegmentAtIndex:1];
}
}];
I was wondering what I did wrong in the following code. The last two lines always return 0. Am I not supposed to get a relation online and set it to a local relation?
The issue is in your line.
self.array = [_query findObjects];
It takes time to update value of this array. You need to add this line of code in block, after getting objects store it in self.array.
You can replace your code with this one.
self.array2 = [[NSMutableArray alloc] init];
self.addedArray2 = [[NSMutableArray alloc] init];
PFQuery *query = [PFQuery queryWithClassName:#"Friends"];
[query whereKey:#"user" containsString:[[PFUser currentUser] objectForKey:#"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for (Friends *currentFriend in objects) {
self.relation = currentFriend.friendsRelation;
self.addedRelation = currentFriend.addedRelation;
self.query = [_relation query];
[[_relation query] findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[self.array2 addObjectsFromArray:objects];
[[_addedRelation query] findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[self.addedArray2 addObjectsFromArray:objects];
[self.segmentControl setTitle:[NSString stringWithFormat:#"Friends (%lu)", (unsigned long)[self.array2 count]] forSegmentAtIndex:0];
[self.segmentControl setTitle:[NSString stringWithFormat:#"Added Me (%lu)", (unsigned long)[self.addedArray2 count]] forSegmentAtIndex:1];
}];
}];
}
}];
Hope it helps. Let me know... :)
Related
I have the following code to find the objects in a custom class. For some reason it is finding two of the same object (self.addedArray2's count is 2, but on Parse data table online, there is only one). Do you see why it is finding the same object twice?
self.array2 = [[NSMutableArray alloc] init];
self.addedArray2 = [[NSMutableArray alloc] init];
PFQuery *query = [PFQuery queryWithClassName:#"Friends"];
[query whereKey:#"user" containsString:[[PFUser currentUser] objectForKey:#"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for (Friends *currentFriend in objects) {
self.relation = currentFriend.friendsRelation;
self.addedRelation = currentFriend.addedRelation;
self.query = [_relation query];
[[_relation query] findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[self.array2 addObjectsFromArray:objects];
[[_addedRelation query] findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[self.addedArray2 addObjectsFromArray:objects];
[self.segmentControl setTitle:[NSString stringWithFormat:#"Friends (%lu)", (unsigned long)[self.array2 count]] forSegmentAtIndex:0];
[self.segmentControl setTitle:[NSString stringWithFormat:#"Added Me (%lu)", (unsigned long)[self.addedArray2 count]] forSegmentAtIndex:1];
NSLog(#"Number 1: %#", [self.addedArray2 objectAtIndex:0]);
NSLog(#"Number 2: %#", [self.addedArray2 objectAtIndex:1]);
}];
}];
}
}];
It is logging "Number 1" then "Number 1" then "Number 2". It looks like that code is being run twice and adding it again. How do I fix that? With all this nesting, I don't really see what I can do.
For your key "user" of the Friends class, if it String that holds the user's username, try replacing:
[query whereKey:#"user" containsString:[[PFUser currentUser] objectForKey:#"username"]];
with this and let me know if that changes anything:
[query whereKey:#"user" equalTo:[[PFUser currentUser] objectForKey:#"username"]];
If it is a pointer to a user object, however, then you will want your query to resemble something along these lines:
[query whereKey:#"user" equalTo:[User currentUser]];
If either of my assumptions are wrong, let me know because I do not know the structure of your classes, but I will try and figure it out.
i am newbie in to Parse.com i try to Fetch data from Parse table with same key but value is Different like as
-(void)getdata
{
NSMutableArray *allObjects = [NSMutableArray array];
NSUInteger limit = 1000;
__block NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:#"MapInfo"];
PFQuery *query = [PFQuery queryWithClassName:#"MapInfo"];
[query whereKey:#"Type" containedIn:#[#"Temopary", #"Business"]];
[query setLimit: limit];
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
[allObjects addObjectsFromArray:objects];
if (objects.count == limit) {
skip += limit;
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[allObjects addObjectsFromArray:objects];
self.lqpinname=[allObjects valueForKey:#"GPIN"];
NSLog(#"Qpin name COunt %#",self.lqpinname);
self.locationArray=[allObjects valueForKey:#"Location"];
self.llatitude=[self.locationArray valueForKey:#"lat"];
self.llongitude=[self.locationArray valueForKey:#"lng"];
self.laddress=[allObjects valueForKey:#"Address"];
self.lusernameArray=[allObjects valueForKey:#"AddedBy"];
hudView.hidden=TRUE;
}];
}
}
else
{
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
But it is return Null value here i want to Fetch data from table where column Type=Business & Temporary Please Give me Solution for this.
thanks.
You should be able to use whereKey:containedIn: to do this.
PFQuery *query = [PFQuery queryWithClassName:#"MapInfo"];
[query whereKey:#"Type" containedIn:#[#"Temopary", #"Business"]];
You also have a typo in Temporary (unless Temporay is something) - not sure if that's intentional or not.
This is basically killing my app and Parse isn't exactly being prompt with their responses.
Queries on pointer arrays aint working.
Try this in your app:
- (void)runTest{
// Run a build to store 20 rows.
for(int i=0;i<20;i++){
NSMutableArray *recipients = [NSMutableArray array];
[recipients addObject:[PFUser currentUser]];
PFObject *message = [PFObject objectWithClassName:#"Run"];
[message addObjectsFromArray:recipients forKey:#"recipients"];
[message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
}];
}
}
- (void)runTestQuery{
// Run a second build to query the 20 rows. Not all 20 are returned.
PFQuery *query = [PFQuery queryWithClassName:#"Run"];
[query whereKey:#"recipients" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSLog(#"Objects count: %li",(long)objects.count);
}];
}
i'm new to parse and i'm trying to fetch a user objectId, but whatever i cant seem to return the objectId. I can easily return the username. this is my query:
PFQuery *query = [PFUser query];
[query whereKey:#"username" equalTo:[[PFUser currentUser] objectForKey:#"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
if (objects.count) {
NSLog(#"%#", [objects lastObject]);
}
}
}];
This returns something like this:
<PFUser:9dcc65tsdr:(null)> {
username = AEleQFdBx9jdtypfsQmLtzAvW;
}
How do i return the objectId which is between PFUser and (null)?
You can use object.objectId like this:
PFQuery *query = [PFUser query];
[query whereKey:#"username" equalTo:[[PFUser currentUser] objectForKey:#"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
if (objects.count) {
for (PFObject *object in objects){
NSLog(#"Object ID: %#", object.objectId);
}
}
}
You can also use this: [[PFUser currentUser]objectId] much faster and works much better because you don't have to run a whole PFQuery.
I am doing a query in parse to check friend request. The query returns objects and I could log them. Now when I convert it to PFUSER, and NSLog, it gets crashed with error: 'Key "FirstName" has no data. Call fetchIfNeeded before getting its value.' Please help me out here.
PFQuery * friendQuery = [PFQuery queryWithClassName:#"friendship"];
[friendQuery whereKey:#"toUser" equalTo:[PFUser currentUser]];
[friendQuery whereKey:#"status" equalTo:#"Pending"];
[friendQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error)
{
//NSLog error
}
else {
NSLog(#"friendRequestCount = %d", objects.count);
PFObject *object=[objects objectAtIndex:0];
NSLog(#"%#", object);
self.friendRequestArray= objects;
NSLog(#"%#",object[#"toUser"]);
PFUser *user1= object[#"toUser"];
NSString *friendName = [NSString stringWithFormat:#"%# %#",user1[#"FirstName"], user1[#"LastName"] ];
NSLog(#"name= %#",friendName);
}
}];
You can use 1 or 2, I have comment the below code, not test.
PFQuery * friendQuery = [PFQuery queryWithClassName:#"friendship"];
[friendQuery whereKey:#"toUser" equalTo:[PFUser currentUser]];
[friendQuery whereKey:#"status" equalTo:#"Pending"];
//1
[friendQuery includeKey:#"toUser"];
[friendQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error)
{
//NSLog error
}
else {
NSLog(#"friendRequestCount = %d", objects.count);
PFObject *object=[objects objectAtIndex:0];
NSLog(#"%#", object);
self.friendRequestArray= objects;
NSLog(#"%#",object[#"toUser"]);
PFUser *user1= object[#"toUser"];
//2 Change PFUser to PFObject
PFObject *user1 = object[#"toUser"];
[user1 fetchIfNeeded];
NSString *friendName = [NSString stringWithFormat:#"%# %#",user1[#"FirstName"], user1[#"LastName"] ];
NSLog(#"name= %#",friendName);
}
}];