Reputation: 153
I don't quite understand how the whereKey, equalTo works.
I've read the documentation at Parse.com and other related questions but don't quite seem to understand how to relate and find the object you are looking for.
What I am trying to do is to delete a PFObject
(an image) after a person has disliked it. With this following code I get the Error
of no results matched the query.
Could someone please explain to me how the linking of this works?
EDITED CODE (NOW FUNCTIONING CORRECTLY):
- (IBAction)likeLook:(id)sender {
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:@"Like.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
NSData *imageData = UIImageJPEGRepresentation(imageFile.image, 1);
[self deleteImage:imageData];
}
else {
[sender setImage:[UIImage imageNamed:@"Liked.png"] forState:UIControlStateSelected];
[sender setSelected:YES];
NSData *imageData = UIImageJPEGRepresentation(imageFile.image, 1);
[self uploadImage:imageData];
NSLog(@"Liked Image");
}
}
-(void)deleteImage:(NSData *)imageData {
PFQuery *query = [PFQuery queryWithClassName:@"UserLikedPhoto"];
[query whereKeyDoesNotExist:@"likedImage"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!object) {
NSLog(@"The getFirstObject request failed.");
} else {
NSLog(@"Successfully retrieved the object.");
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded && !error) {
NSLog(@"Image deleted from Parse");
} else {
NSLog(@"error: %@", error);
}
}];
}
}];
}
-(void)uploadImage:(NSData *)imageData {
PFFile *likedImage = [PFFile fileWithName:@"Image.jpg" data:imageData];
[likedImage saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
PFObject *userLikedPhoto = [PFObject objectWithClassName:@"UserLikedPhoto"];
[userLikedPhoto setObject:likedImage forKey:@"likedLook"];
userLikedPhoto.ACL = [PFACL ACLWithUser:[PFUser currentUser]];
PFUser *user = [PFUser currentUser];
[userLikedPhoto setObject:user forKey:@"User"];
[userLikedPhoto saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
NSLog(@"Saved");
} else {
NSLog(@"Error: %@%@", error, [error userInfo]);
}
}];
} else {
NSLog(@"Error: %@%@", error, [error userInfo]);
}
}];
}
Upvotes: 1
Views: 1719
Reputation: 1685
You probably want to change deleteImage:
to accept the objectId rather than NSData. You can't query parse for a matching data blog (I'm pretty sure anyway, either way it's bad practice).
-(void)deleteImage:(NSString *)objectId {
PFQuery *query = [PFQuery queryWithClassName:@"UserLikedPhoto"];
[query getObjectInBackgroundWithId:objectId block:^(PFObject *object, NSError *error) {
if (!object) {
NSLog(@"The getFirstObject request failed.");
} else {
NSLog(@"Successfully retrieved the object.");
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded && !error) {
NSLog(@"Image deleted from Parse");
} else {
NSLog(@"error: %@", error);
}
}];
}
}];
}
In this case you don't even need the whereKey filter on the query because we're looking for a specific object. You can store the objectId in the saveInBackgroundWithBlock success block under upload image. I hope that helps.
EDIT
From the Parse PFFile Documentation:
You can delete files that are referenced by objects using the REST API. You will need to provide the master key in order to be allowed to delete a file.
If your files are not referenced by any object in your app, it is not possible to delete them through the REST API. You may request a cleanup of unused files in your app's Settings page. Keep in mind that doing so may break functionality which depended on accessing unreferenced files through their URL property. Files that are currently associated with an object will not be affected.
I've never used Parse files but this does seem pretty odd to me. You must use the Master Key to delete files so I would not recommend doing it in your iOS app. You can create a Cloud Code function that handles the delete for you if you'd like or you can just delete the UserLikedPhoto object and periodically request file cleanup.
Upvotes: 1