Search code examples
iosobjective-cparse-platformtimeline

IOS parse query help for two classes Photo and Video


I am newer to parse and IOS and I am trying to display videos and photos in my timeline. I am extending a PFQueryTableViewController and in the queryForTable trying to define a query to pull all Photos and Videos that have been posted by the users which the current user follows, or posts that have been made by the current user himself. I am having trouble with the query but here is what I am trying to do:

parseClassName is set to the Photo Class:

// self.parseClassName = @"Photo";

Then in the queryForTable:

- (PFQuery *)queryForTable {
if (![PFUser currentUser]) {
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    [query setLimit:100];
    return query;
}

PFQuery *followingActivitiesQuery = [PFQuery queryWithClassName:kFTActivityClassKey];
[followingActivitiesQuery whereKey:kFTActivityTypeKey equalTo:kFTActivityTypeFollow];
[followingActivitiesQuery whereKey:kFTActivityFromUserKey equalTo:[PFUser currentUser]];
followingActivitiesQuery.cachePolicy = kPFCachePolicyNetworkOnly;
followingActivitiesQuery.limit = 100;

PFQuery *photosFromFollowedUsersQuery = [PFQuery queryWithClassName:self.parseClassName];
[photosFromFollowedUsersQuery whereKey:kFTPhotoUserKey matchesKey:kFTActivityToUserKey inQuery:followingActivitiesQuery];
[photosFromFollowedUsersQuery whereKeyExists:kFTPhotoPictureKey];

/* Load videos from followed users
PFQuery *videosFromFollowedUsersQuery = [PFQuery queryWithClassName:self.parseClassName];
[videosFromFollowedUsersQuery whereKey:kFTVideoUserKey matchesKey:kFTActivityToUserKey inQuery:followingActivitiesQuery];
[videosFromFollowedUsersQuery whereKeyExists:kFTVideoKey];
*/
PFQuery *photosFromCurrentUserQuery = [PFQuery queryWithClassName:self.parseClassName];
[photosFromCurrentUserQuery whereKey:kFTPhotoUserKey equalTo:[PFUser currentUser]];
[photosFromCurrentUserQuery whereKeyExists:kFTPhotoPictureKey];

/* Load current users videos
PFQuery *videosFromCurrentUserQuery = [PFQuery queryWithClassName:self.parseClassName];
[videosFromCurrentUserQuery whereKey:kFTVideoUserKey equalTo:[PFUser currentUser]];
[videosFromCurrentUserQuery whereKeyExists:kFTVideoKey];
*/

PFQuery *query = [PFQuery orQueryWithSubqueries:[NSArray arrayWithObjects:photosFromFollowedUsersQuery, /*videosFromFollowedUsersQuery,*/
                                                                          photosFromCurrentUserQuery, /*videosFromCurrentUserQuery,*/ nil]];
[query includeKey:kFTPhotoUserKey];
/*[query includeKey:kFTVideoUserKey];*/
[query orderByDescending:@"createdAt"];

// A pull-to-refresh should always trigger a network request.
[query setCachePolicy:kPFCachePolicyNetworkOnly];

// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
//
// If there is no network connection, we will hit the cache first.
if (self.objects.count == 0 || ![[UIApplication sharedApplication].delegate performSelector:@selector(isParseReachable)]) {
    [query setCachePolicy:kPFCachePolicyCacheThenNetwork];
}

return query;
}

This code can be found in parses atypic example.

Any help appreciated!


Solution

  • I ended up resolving this by merging the photos and video parse classes into one "Post" class.