A project I have inherited currently uses Parse for backend. I am trying to migrate the way the data is fetched/posted, from the Parse iOS SDK to using the Parse REST API (using AFNetworking
).
I was able to convert a simple PFQuery
like the following to it's REST equivalent:
PFQuery
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.'999Z'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSString *createdAt = [dateFormatter stringFromDate:createdAtDate];
PFQuery *query = [PFQuery queryWithClassName:@"Photo"];
[query whereKey:@"createdAt" lessThan:createdAt];
[query orderByDescending:@"createdAt"];
[query setLimit:10];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
//Etc...
}
}];
REST API Request:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.'999Z'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSString *createdAt = [dateFormatter stringFromDate:createdAtDate];
NSString *dateQueryString = [NSString stringWithFormat:@"{\"createdAt\":{\"$lte\":{\"__type\":\"Date\",\"iso\":\"%@\"}}}", createdAt];
NSDictionary* parameters = @{@"order": @"-createdAt",
@"limit": @10,
@"include": @"user",
@"where":dateQueryString};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer parseSerializer]; // Already has App-Id, App-key and Content-Type set.
NSMutableURLRequest* request = [manager.requestSerializer requestWithMethod:@"GET" URLString:@"https://api.parse.com/1/classes/Photo" parameters:parameters error:nil];
AFHTTPRequestOperation* operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Etc..
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error.localizedDescription);
}];
[operation start];
The problem however is with compound queries like the one below:
NSMutableArray* subqueriesArray = [NSMutableArray array];
PFQuery *followingActivitiesQuery = [PFQuery queryWithClassName:@"Activity"];
[followingActivitiesQuery whereKey:@“type” equalTo:@“Follow”];
[followingActivitiesQuery whereKey:@“fromUser” equalTo:[PFUser currentUser]];
followingActivitiesQuery.limit = 1000;
PFQuery *privateUserQuery = [PFUser query];
[privateUserQuery whereKey:@“yourMomLikedThisPhoto” equalTo:[NSNumber numberWithBool:NO]]; // fake whereKey of course
PFQuery *photosFromFollowedUsersQuery = [PFQuery queryWithClassName:@"Photo"];
[photosFromFollowedUsersQuery whereKey:@“yourMomLikedThisPhoto” equalTo:[NSNumber numberWithBool:YES]];
[photosFromFollowedUsersQuery whereKey:@“user” matchesKey:@“toUser” inQuery:followingActivitiesQuery];
[photosFromFollowedUsersQuery whereKey:@“user” matchesQuery:privateUserQuery];
PFQuery *photosFromCurrentUserQuery = [PFQuery queryWithClassName:@"Photo"];
[photosFromCurrentUserQuery whereKey:@“yourMomLikedThisPhoto” equalTo:[NSNumber numberWithBool:YES]];
[photosFromCurrentUserQuery whereKey:@“user” equalTo:[PFUser currentUser]];
[subqueriesArray addObject:photosFromFollowedUsersQuery];
[subqueriesArray addObject:photosFromCurrentUserQuery];
PFQuery *query = [PFQuery orQueryWithSubqueries:subqueriesArray];
[query includeKey:@"Photo"];
[query orderByDescending:@"createdAt"];
If someone could help me understand how to construct the parameters
for this, I'd really appreciate it. A general idea on how to proceed in the right direction will also help. I've referred the Parse REST API Guide, but I can't seem to get it. I'm trying to get it right using $inQuery
, but no luck yet.
An answer to this question will cover most of the common and complex issues one faces when querying with the Parse REST API (on iOS).