I have an array in the Data Browser that is supposed to have a list of users who've received an item from a user. The actual content of the tile is
[{"__type":"Pointer","className":"_User","objectId":"3zQoMVRJOx"}]
I can't figure out how to actually call,use, and display this data from Xcode.
My end goal is to be able to find the total number of users who've been sent an item so this is why I need the content from the array. Any help would be great. Im sure it is probably a simple line of code that I'm not seeing.
Your question actually qualifies for a "too broad" flag, as it seems you haven't tried this yourself and are experiencing problems, but are asking us to supply the code for you. It is not just a simple line of code.
However, I will supply you with code snipped from the ios guide over at Parse, slightly altered to get the array you're after:
PFQuery *query = [PFQuery queryWithClassName:#"GameScore"];
[query whereKey:#"playerName" equalTo:#"Dan Stemkoski"];
[query includeKey:#"receivers"]; // Force parse to include the user objects of receivers
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(#"Successfully retrieved %d scores.", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
// Write to log the email of every receiver
for (PFUser *receiver in object[#"receivers"]) {
[receiver fetchIfNeeded]; // fetches the object if it is still just a pointer (just a safety; it should be already included by the includeKey call earlier in the code
NSLog(#"Receiver: %#", receiver[#"email"]);
}
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
Good luck :-)
Related
I am facing an issue when trying to update data in Parse.
Here is my code that i am using.
PFQuery *query = [PFQuery queryWithClassName:#"GameScore"];
[query whereKey:#"name" equalTo:#"Username"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *item, NSError *error)
{
if (!error)
{
[item setObject:#500 forKey:"Score"];
}
[item saveInBackgroundWithBlock:^(BOOL succeeded, NSError *itemError)
{
if (!error)
{
NSLog(#"Score is updated");
}
else
{
NSLog(#"Error in updating: %#",error.description);
}
}];
}
else
{
NSLog(#"Error in getting Score: %#",error.description);
}
}];
This code works only when i create a new PFObject and then try to update it.
But,when i exit my app and then try to update the score again,i am unable to update the data.It throws this error..
Error in getting Score: Error Domain=Parse Code=101 "No results matched the query." UserInfo={error=No results matched the query., NSLocalizedDescription=No results matched the query., code=101}
It works again,if i create a new PFObject.
Please help,i am new to Parse and am still try to understand it.
Thank you.
You need to use findObjectsInBackgroundWithBlock instead of getFirstObjectInBackgroundWithBlock as the latter can only be used if there's at least 1 object.
Reference - Parse Questions
You could also use the PFQuery's count method. If the count is >= 1, use getFirstObjectInBackgroundWithBlock, otherwise display a message / handle that case however you'd like.
Other options include storing the objectId of the GameScore object associated with a player on their user object, creating an object without data using the objectId, then fetching it. Or simply use a pointer, but that can do weird things when saving / querying / fetching.
I am testing out Parse localDatastore and am struggling with refreshing the local datastore after a new server PFQuery.
The PFQuery works fine and seems to pin the array to the local datastore just fine. When I change the contents of the array on the server, the server PFQuery pulls down the updated array, but the local datastore doesn't seem to update:
- (void)viewDidLoad {
[super viewDidLoad];
// Query Parse
PFQuery *query = [PFQuery queryWithClassName:#"contacts"];
NSArray *objects = [query findObjects];
[PFObject pinAllInBackground:objects block:^(BOOL succeeded, NSError *error) {
if(succeeded) {
NSLog(#"Successfully retrieved %lu records from Parse.", (unsigned long)objects.count);
} else if (error) {
NSLog(#"Error");
}
}];
}
and then a UIButton is used to log the contents of the local datastore to the console:
-(IBAction)showDatastore {
// Query the Local Datastore
PFQuery *query = [PFQuery queryWithClassName:#"contacts"];
[query fromLocalDatastore];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"Successfully retrieved %lu contacts from Datastore.", (unsigned long)objects.count);
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
In my sample the original array has 15 objects. Both the count's from each array initially are 15. I then remove an object from the server array and the initial PFQuery count is 14, but the local datastore count remains 15.
Parse's documentation states:
When an object is pinned, every time you update it by fetching or saving new data, the copy in the local datastore will be updated automatically.
But that doesn't seem to be the case... at least not with this recommended code. Is there something i'm missing?
It depends on how you are deleting the object. If you're using deleteEventually, then the deletion will propagate to the LDS
You can query from the local datastore using exactly the same kinds of queries you use over the network. The results will include every object that matches the query that's been pinned to your device. The query even takes into account any changes you've made to the object that haven't yet been saved to the cloud. For example, if you call deleteEventually, on an object, it will no longer be returned from these queries.
But any other method requires explicit unpinning if you want it to work.
deleteEventually is the prefered method I believe.
I am using below code to fetch users but I am not able to get it.. app is crashes... Please help me to get all installation objects list..
PFQuery *userQuery = [PFQuery queryWithClassName:#"_Installation"];
[userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(#"Successfully retrieved %d scores.", objects.count);
NSLog(#"objc...%#",objects);
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(#"id...%#",object.objectId);
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
Try this:
PFQuery *userQuery = [PFInstallation query];
[userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(#"Successfully retrieved %d scores.", objects.count);
NSLog(#"objc...%#",objects);
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(#"id...%#",object.objectId);
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
FYI, Wain is correct in his comment that installations are not users. Are you sure this is the class you want to query?
With Parse, as flexible and sound it is, they are limitations to it. Some are extremely worrying in my opinion as a developer that uses Parse, and some are just implemented server side to protect your end-users. This is one, you can not query the Installation class from a client, the only columns you can query are listed in the API Reference . However, you can query the class through a cloud function using the master key, otherwise, you will have to use a pointer/relation to other tables for whatever data you want to retrieve. Additionally, for future question seekers, please refer to Wains note. It's a valid statement and should be considered prior to proceeding with anything. Users are not installations, the same 'user' i.e., device, can re-download the app multiple times creating numerous installations (not users).
I dont know what the deal with parse is but for some reason it wont allow me to save the retrieved array into a mutable array I created. It works inside the parse code block but once outside, it displays null. Help please?
PFQuery *query = [PFQuery queryWithClassName:#"comments"];
[query whereKey:#"flirtID" equalTo:recipe.flirtID];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
comments = [[NSMutableArray alloc]initWithArray:objects];
// Do something with the found objects
for (PFObject *object in objects) {
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
NSLog(#"%#",[comments objectAtIndex:0]);
It's actually working as it should.
You should read up on how blocks work.
Edit: Try reading Apple's Documentation
You're NSLogging 'comments' before comments actually gets set. How does that work?
You see, query is running in the background, and it will actually take a bit of time. It's running asynchronously. But the code outside the block will run immediately.
While the code comes before, because it's an asynchronous block, it can and will be run whenever.
Try this:
comments = [[NSMutableArray alloc]initWithArray:objects];
NSLog(#"%#",[comments objectAtIndex:0]);
The important question is, what do you want to do after the query? Looks like you want to save comments, but then what? That will determine what you do next.
I'm trying to create a function in an app that will let the user pick a time frame like past 24 hours or past week and see results that only go back that far. I've tried so many things that I'm not even sure what I'm doing anymore and I'd like some help.
PFQuery *dateQuery = [PFQuery queryWithClassName:#"NewMarcoPolo"];
[dateQuery whereKey:#"createdAt" greaterThan:yesterday];
[dateQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
//NSLog(#"Successfully retrieved %d scores.", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
// NSLog(#"%#", object.objectId);
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
NSLog(#"%#", dateQuery);
}];
I'm also not sure how to know if it works or not. I tried logging it in the console but as an array I only get PFQuery: 0x9b55030. If anyone has an idea how to get my desired results I'd be very grateful. And if anyone has tips for how to implement this, it'd be great.