I think it's very trivial, but i can't figure it out why these codes are incorrect.
I want to retrieve one image from the Parse User class. I mean, every user has a profile picture, that i store under the "imageFile" column and i would like to load it to a PFImageView. The upload works correctly, but i'cant retrieve it. The log says error, because there is no matches for the query. The file what i want to retrieve exists, so it's 100% that my query implementation is wrong. I would really appreciate any relevant help, thanks.
Non query version: (all versions are implemented in the viewDidLoad)
self.userThumbnail.file = self.currentUser.imageFile;
[self.userThumbnail loadInBackground];
query version
PFQuery *queryImage = [PFQuery queryWithClassName:#"User"];
[queryImage whereKey:#"imageFile" equalTo:self.currentUser]
[queryImage getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (error) {
NSLog(#"ERROR");
} else {
PFFile *file = [object objectForKey:#"imageFile"];
self.userThumbnail.file = file;
[self.userThumbnail loadInBackground];
}
}];
query version
PFQuery *queryImage = [PFUser query];
[queryImage whereKey:#"imageFile" equalTo:self.currentUser];
[queryImage getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (error) {
NSLog(#"ERROR");
} else {
PFFile *file = [object objectForKey:#"imageFile"];
self.userThumbnail.file = file;
[self.userThumbnail loadInBackground];
}
}];
This line:
[queryImage whereKey:#"imageFile" equalTo:self.currentUser];
looks like a mistake. It says, "find Users whose imageFile column (presumably a PFImage) is equal to the current user object". That will never be true.
Try to separate the logic for finding a user from dealing with it's imageFile. In other words. First get a user:
PFQuery *queryUser = [PFQuery queryWithClassName:#"User"];
[queryUser whereKey:#"username" equalTo:#"some-user-name"];
[queryUser getFirstObjectInBackgroundWithBlock:^(PFObject *user, NSError *error) {}];
// or maybe instead of username==, get a user with their id ...
[queryUser getObjectInBackgroundWithId:#"some-user-id" block:^(PFObject *user, NSError *error) {}];
Now, inside the completion block for querying a user, do your file logic:
[queryUser getFirstObjectInBackgroundWithBlock:^(PFObject *user, NSError *error) {
PFFile *userImageFile = user[#"imageFile"];
[userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:imageData];
// this is a regular UIImage now, put it in your UI
self.someUIImageView.image = image;
}
}];
}];
Related
I have a class called "images" on my parse.com app, this class has a column called "imageFile" which contains a .png image, what I'm wondering is how do i set parseImage to be the image taken in from parse. This is what I have so far.
PFQuery *query = [PFQuery queryWithClassName:#"images"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
NSLog(#"Retrieved data");
if (!error) {
PFFile *file = [object objectForKey:#"imageFile"];
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
parseImage = [UIImage imageWithData:data];
// image can now be set on a UIImageView
}
}];
//parseImage.file = file;
//[imageHolder loadInBackground];
}
}];
parseImage.file = file;
[parseImage loadInBackground];
PFImageView loads asynchronously. Use that instead of manually retrieving the file.
I'm using the Complete iOS 7 course on Udemy and am having a problem with Facebook login persistence. I working on the MatchedUp app.
When I delete the app from my simulator and login with a test user the original name and profile picture persists. When checking Parse data it has clearly created the second user, user permissions, and profile picture. It's not reflected in my app.
Can someone please point me in the right direction?
Is there a method that needs to be called to disable parse push?
Here is where I query Photo:
PFQuery *query = [PFQuery queryWithClassName:#"Photo"];
*//[query whereKey:#"user" equalTo:[PFUser currentUser]];*
[query includeKey:kCCPhotoUserKey];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error)
{
self.photos = objects;
[self queryForCurrentPhotoIndex];
}
else
{
NSLog(#"%#", error);
}
}];
and this is where I create Photo:
PFFile *photoFile = [PFFile fileWithData:imageData];
*//PFFile *photoFile = [PFFile fileWithName:[PFUser currentUser][#"user"] data:imageData];*
[photoFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
PFObject *photo = [PFObject objectWithClassName:kCCPhotoClassKey];
[photo setObject:[PFUser currentUser] forKey:kCCPhotoUserKey];
[photo setObject:photoFile forKey:kCCPhotoPictureKey];
[photo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
NSLog(#"Photo Saved Successfully");
}];
}];
I am trying to do a nested block inside of another block. It seems that possibly asynchronous requests time out a lot fast on iOS8 as opposed to previous version of iOS. Here is my Parse code:
- (void)getRemoteImageAndSaveLocalWithObjectId:(NSString *)objectId andType:(NSString *)type{
if ([type isEqualToString:#"user"]) {
PFQuery *query = [GAUser query];
//[query includeKey:#"userImage"];
[query getObjectInBackgroundWithId:objectId block:^(PFObject *object, NSError *error) {
GAUser *gaUser = (GAUser *)object;
PFFile *userImageFile = gaUser[#"#userImage"];
[userImageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
// I am never able to get inside this block of code.
NSLog(#"in get data");
if (!error) {
NSLog(#"remote image name: %#",data.debugDescription);
UIImage *image = [UIImage imageWithData:data];
}else{
NSLog(#"there was an error");
}
}];
}];
}
}
Does anyone know why I can't get in the getDataInBackgroundWithBlock block?
Darren and Andy were right (they're probably brothers).
userImageFile was nil and that was the problem.
I set up a profile picture for users following the code here, but I can't seem to retrieve the picture. The code I tried is below please help me out can't seem to figure this out:
PFQuery *query = [PFUser query];
[query getObjectInBackgroundWithId:#"profilePic" block:^(PFObject *object, NSError *error) {
if (!error) {
NSData * imageData = UIImagePNGRepresentation(object);
}
else{
// Error stuff
}
}];
You've misunderstood the purpose of getObjectInBackgroundWithId:block: ... the first parameter is meant to be the ID of the object you want to get. Inside the block you can then query for properties of the object you've gotten.
e.g. if you want the current user:
PFQuery *query = [PFUser query];
[query getObjectInBackgroundWithId:[PFUser currentUser].objectId block:^(PFObject *user, NSError *error) {
if (!error) {
PFFile *profilePic = user[#"profilePic"];
[profilePic getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:imageData];
}
}];
}
else{
// Error stuff
}
}];
I am using Parse for an iOS app and it requires me to load multiple image in one PFObject, and so I have multiple PFFiles in that object. I know you have to load each object seperately in order to use it, but is there a way to load multiple PFFiles at once from an object? Any input is appreciated!
for other new folks like me, i believe the accepted answer from 'ahar083' may have a typo in it. i changed 'data1' to 'file1' and 'data2' to 'file2' (updated code = pasted below). i do not have enough points to comment on an answer = posted my comment as a new answer.
PFQuery *query = [PFQuery queryWithClassName:#"ManyFileClass"];
[query getObjectInBackgroundWithId:#"OBJECT_ID"
block:^(PFObject *manyFileClass, NSError *error) {
PFFile* file1 = [o objectForKey:#"file1"];
PFFile* file2 = [o objectForKey:#"file2"];
[file1 getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
NSLog(#"Got data from file1#");
}
}];
[file2 getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
NSLog(#"Got data from file2#");
}
}];
}];
When you retrieve a PFObject, you don't actually pull down any PFFiles pointed to within that object anyway, you always need to manually pull down the file, whether it is 1 file or 5 files.
PFQuery *query = [PFQuery queryWithClassName:#"ManyFileClass"];
[query getObjectInBackgroundWithId:#"OBJECT_ID"
block:^(PFObject *manyFileClass, NSError *error) {
PFFile* file1 = [o objectForKey:#"file1"];
PFFile* file2 = [o objectForKey:#"file2"];
[data1 getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
NSLog(#"Got data from file1#");
}
}];
[data2 getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
NSLog(#"Got data from file2#");
}
}];
}];