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.
Related
Hi I am trying to query some files from my Parse database and I want the files to be sorted according to the updateAt time. I have the following code. The query works and the results are sorted according to my condition, but when I load the files using getDataInBackground and then add to an array. The files are not sorted and they appear to be random in the array.
So My questions are
What can I do to make sure the files in the array are in the same order as the query results?
Any way to check the files/images against the objectID in the completion block of getDataInBackground?
p.s. I don't want to use getData since I don't want it to block the main thread.
Thank you very much in advance
PFQuery *query = [PFQuery queryWithClassName:#"Photo"];
[query orderByDescending:#"updateAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *photoStacks, NSError *error)
{
if (!error) {
// The find succeeded.
for (PFObject *photoImage in photoStacks) {
PFFile *userImageFile = photoImage[#"image"];
[userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:imageData];
// need to check object id before adding into the stack to make sure the order is right
[photoImageStacks addObject:image];
if ([photoImageStacks count] == photoStacksCount)
{
[photoPile setArray:photoImageStacks];
}
}
}];
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
use breakpoint and trace first photoImageStacks and second after response you should call reload method if you are using tableview or some delegate or fire a notification so that you can update ui accordingly after successful response.
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 am running the query detailed below. It does not log any error and proceeds to log the objects array. When it logs the objects array it displays every object within it up until the first object that was added to the class today. No objects other than the first one added today are displayed. If I delete the first object from today it will display every object up until the new first object for today.
PFQuery *query = [PFQuery queryWithClassName:#"className"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"%#", objects);
}
} else {
NSLog(#"Error: %#", error);
}
}];
I wasn't able to figure out why this was occurring so I simply created a new class and added all of the values from my old class to it and it began to work.
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 :-)