How can I get all pointer class value Parse? - ios

I have two table Order and Modifiers. Modifier is the pointer class for the Order. Modifiers has a column (name ,price etc). I want to get all the values of Modifiers with order.
Code
PFQuery *queryLocal= [PFQuery queryWithClassName:#"order"];
[queryLocal whereKey:#"order_no" equalTo:orderno];
[queryLocal includeKey:#"modifiers"];
[queryLocal fromLocalDatastore];
[queryLocal findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (block) {
block(objects,nil);
}
}];
Output :
<modifiers: 0x7a15dc30, objectId: ZgikpV86HB, localId: (null)> {
}
It gives me only the objectId. is there any way to get all values

Related

Pagination With Dynamic Content

I'm trying to figure out the proper way to paginate results using the Parse SDK for iOS. The problem I'm having is that the content I'm trying to paginate has the potential to change frequently.
For example:
There is an app with a bunch of objects stored on a server, and the request to get these objects is sorted with newest first and paginated in case there are a lot of objects. When the user scrolls, as they are approaching the last object on the current page, the next page gets requested.
Say the user scrolls to page two, then a new object is added to the list of objects, and the user then scrolls up another page. When the next page is requested, they will get a duplicate of the last message on the second page, and since page 0 has already been loaded, the new object won't be displayed.
How can something like this be handled?
How would the implementation change if the objects were sorted oldest first?
Thanks in advance for the help.
Edit: request pseudo code
- (void)fetchObjectsForPage:(NSUInteger)page completion:(void (^)(NSArray *_Nullable objects, PageInfo *_Nullable pageInfo, NSError *_Nullable error))completion{
PFQuery *query = [SomeObject query];
[query orderByAscending:#"updatedAt"];
[query setLimit:50];
[query setSkip:50 * page];
[query findObjectsInBackgroundWithBlock:^{NSArray *_Nullable objects, NSError *_Nullable error){
...
>>> Do error checking and return array along with paging info (total number of objects, page size) or error in completion block.
}];
}
It's not the request I'm having trouble with, it's figuring out how to properly handle paging in the table when a new object gets added.
What you can do is the following:
Create a query that will return 50 rows (like you did) order by updatedAt
When you get result take the last item from the list and save the item updatedAt in some global property
In the next call do not use setOffset but get the next 50 rows and add another condition of where updatedAt is greater than the updatedAt of your global object. This way you make sure that you will not have duplicate records in your list
At the end your code should look like the following:
PFQuery *query = [PFQuery queryWithClassName:#"SomeClassName"];
[query orderByAscending:#"updatedAt"];
[query setLimit:50];
if (self.lastItemUpdatedAt){
[query whereKey:#"updatedAt" greaterThan:self.lastItemUpdatedAt];
}
[query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
if (objects.count > 0){
self.lastItemUpdatedAt = [[objects lastObject] updatedAt];
}
}];
Now if you use updatedAt there is a chance that you will still get duplicates because let's assume that someone change the object so the updatedAt will also be changed (so it will be greater than the updatedAt that you saved) in this case you can use createdAt instead of updatedAt since createdAt will never be changed

Programmatically deleting data associated with PFUser.objectID if PFUser is deleted

So in my app I am using Parse for user accounts. I have a class "Follow" with "to" and "from" fields containing user object ids, allowing users to follow each other. Now, if I delete a user somehow, the follow relation remains in tact and querying empty user data raises an object not found error. What I want to know is how I can delete a follow relation if any of the "to" or "from" fields contains an objectID of an object that does not exist.
I have tried querying objects by their objectID, but any attempt at checking empty data (like checking if a user.username is nil) has resulted in a missing object error, and I can't check if an object is nil because Xcode says it never will be.
Thanks!
I think you need to acquire it manually, if you have deleted user, then you can delete all of its relations to maintain Database integrity, following is the code you would calling after deleting any User.
PFQuery * _to = [PFQuery queryWithClassName:#"Follow"];
[_to whereKey:#"to" equalTo:#"Replace UserID of Deleted User"];
PFQuery * _from = [PFQuery queryWithClassName:#"Follow"];
[_from whereKey:#"from" equalTo:#"Replace UserID of Deleted User"];
PFQuery *query = [PFQuery orQueryWithSubqueries:#[_to, _from]];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(#"Successfully retrieved %d relations.", results.count);
// Do something with the found objects, lets delete them all to intact relationship
[PFObject deleteAllInBackground:results];
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
Please follow this link for detailed description and information:
Parse Compound Queries

Parse GeoQuery 'withinKilometres' is not supported

Here's an interesting one:
I have a UIPickerView that contains the values "10 km", "25 km", "50 km" and "100 km". What ever value is selected will be the value [queryLocation ... withinKilometers: value here ]; is equal too.
Since the value for 'withinKilometers' needs to be a double value, I have the following code to convert the X km to just X as a double value:
if ([self.selectedData isEqualToString:#"10 km"]) {
self.selectedDataConverted = #"10";
self.stringToDouble = [self.selectedDataConverted doubleValue];
NSLog(#"Double Value: %f", self.stringToDouble);
}
... and so on...
and here's the code for the query:
PFQuery *queryLocation = [PFUser query];
[queryLocation whereKey:#"location" nearGeoPoint:self.userLocation withinKilometers:self.stringToDouble];
if (!error) {
NSLog(#"Great Success");
//Do the stuff here
}
else {
NSLog(#"Error");
}
}];
Now when I run this, I get the error [Error]: geo query within or is not supported (Code: 102, Version: 1.7.4). I'm stumped and am not sure how to fix this. Can't find anything similar to my problem either online.
EDIT: I'm starting to think maybe it's because I have connected 2 queries into 1:
PFQuery *finalQuery = [PFQuery orQueryWithSubqueries:#[queryMale, queryLocation]];
[finalQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"Great Success");
}
else {
NSLog(#"Error");
}
}];
This is to find accounts with the gender 'male' within the desired kilometres at the same time.
You're right. The error geo query within or is not supported means you can't use this query with orQueryWithSubqueries. You will need to run these as 2 separate queries and then combine the result sets yourself.

getObjectInBackgroundWithId: using pointer [ Parse ]

First i get the current user and the connected book to that user (It's a pointer to a book objectId). Then i want to find the book using the getobjectinBackgroundWithId method and then display the book name. When i change getObjectInBackgroundWithId:storeUserID to getObjectInBackgroundWithId:#"f4Dg92xC2" it works perfect!
PFUser *currentuser = [PFUser currentUser];
storeUserID = currentuser[#"connectedBook"]; //Store book id in a string
NSLog(#"%#", storeUserID); // what i get <Book:f4Dg92xC2:(null)>
PFQuery *query = [PFQuery queryWithClassName:#"Book"];
[query getObjectInBackgroundWithId:storeUserID block:^(PFObject *books, NSError *error){
PFObject *bookObject = books[#"bookName"];
NSLog(#"bookName:%#", bookObject);
}];
when i run this, the NSLog with the book name, shows:
Error: bad special key: objectId (Code: 102, Version: 1.2.20)
2014-09-17 23:28:56.529 MyApp[18977:90b] bookName:(null)
Thanks a lot in advance!!
Your comment //Store book id in a string is incorrect. A pointer doesn't return the string object id, it returns the object itself. So you already have it and you don't need to call getObjectInBackgroundWithId:.
The log <Book:f4Dg92xC2:(null)> even tells you it's a Book instance (not an NSString instance).

how to get most recently added parse.com object

Is there any way to do a query in Parse to get the most recently added PFObject type?
I know that I can do a query with a greater than criteria, but it would be very useful if there was a function to get the most recent object added when no date is known.
Just add a descending order on createdAt and get the first object:
PFQuery *query = [PFQuery queryWithClassName:#"YourClassName"];
[query orderByDescending:#"createdAt"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
// code
}];
In Swift:
var query = PFQuery(className: "YourClassName")
query.orderByDescending("createdAt")
query.getFirstObjectInBackgroundWithBlock {(object: PFObject?, error: NSError?) -> Void in
// code
}
This may not address the use case in question. I often want to get back the objectId of the object i just added in code. While it might be automatically possible, i have missed any info in this regard. So i usually have my own unique identifier that i add, and then retrieve the object by that identifier in the async block.. thus getting the objectId..

Resources