I have a Parse classes "Activity" and "Photo". How to fetch NSArray of PFObjects liked images from currentUser?
PFQuery *queryLikedPhoto = [PFQuery queryWithClassName:@"Activity"];
[queryLikedPhoto whereKey:@"type" equalTo:@"like"];
[queryLikedPhoto whereKey:@"fromUser" equalTo:[PFUser currentUser]];
[queryLikedPhoto includeKey:@"photo"];
[queryLikedPhoto findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
PFObject *photoObject = [object objectForKey:@"photo"];
[photoObject fetchIfNeededInBackgroundWithBlock:^(PFObject *photoObjects, NSError *error) {
NSLog(@"photoObjects: %@", photoObjects);
}];
}
}
}];
And it retrieve PFOjects with images, but how to save it to NSArray for further using?! (variants with NSMutableArray doesn't work):
PhotoObjects: <Photo: 0x166baf30, objectId: Q0H7XKYeCU, localId: (null)> {
image = "<PFFile: 0x166ba810>";
thumbnail = "<PFFile: 0x166ba450>";
user = "<PFUser: 0x1668c6f0, objectId: qGYdDnAtbD, localId: (null)>";
username = "Tim";
}
2015-10-18 23:39:57.058 MyApp[5817:572564] photoObjects: <Photo: 0x166bdec0, objectId: eWGonc9YLz, localId: (null)> {
image = "<PFFile: 0x166bd9e0>";
thumbnail = "<PFFile: 0x166bd6a0>";
user = "<PFUser: 0x1668c6f0, objectId: qGYdDnAtbD, localId: (null)>";
username = "Steve";
}
This query looks better Edit:
PFQuery *queryLikedPhoto = [PFQuery queryWithClassName:@"Activity"];
[queryLikedPhoto whereKey:@"type" equalTo:@"like"];
[queryLikedPhoto whereKey:@"fromUser" equalTo:[PFUser currentUser]];
[queryLikedPhoto whereKeyExists:@"photo"];
[queryLikedPhoto findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.likedPhotoObjects = [NSMutableArray array];
if (objects.count >0) {
for (PFObject *object in objects) {
PFObject *photoObject = [object objectForKey:@"photo"];
[self.likedPhotoObjects addObject:photoObject];
}
}
[self.collectionView reloadData];
}
}];
EXTRACT IMAGE FROM PFQUERY
You appear to be treating your " photo " PFFile object as a PFObject instead of a PFFile.
Insert the following code within your query block :
PFFile *imageFile = [object objectForKey:@"photo"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error)
{
if (!error)
{
// SUCCESS.
imageView.image = [UIImage imageWithData:data];
}
else
{
// ERROR.
NSLog(@"%@", error);
}
}];
SAVE SAID IMAGE
I would use an image cache Cocoa Pod [ ie. SAM CACHE ].
When I use SAM Cache saving an image file is as simple as :
[[SAMCache sharedCache] setImage:[UIImage imageWithData:data] forKey:imageKey];
wherein : imageKey refers to a unique NSString attached to the object within the data table [ ie. the ObjectId ].
RETRIEVE SAID IMAGE
To retrieve the image file in the future I then execute the following line of code :
UIImage *cacheImage = [[SAMCache sharedCache] imageForKey:imageKey];
CHECK WHETHER IMAGE IS IN CACHE
To check whether an image exists within the cache for the imageKey
UIImage *cacheImage = [[SAMCache sharedCache] imageForKey:imageKey];
if ( cacheImage )
{
// Image exists within cache.
}
else
{
// Image does not exist within cache.
}
ALTERNATIVES FOR CACHING
Parse.com also provides a cache feature referenced within their SDK.
Please refer to the following link for further info :
https://www.parse.com/docs/ios/guide#queries-caching-queries