iPhone - PFRelation 未保存对象
iPhone - PFRelation Is Not Saving Object
我是解析新手,正在尝试创建一个将用户链接到 his/her 图片的 PFRelation。这是我的代码:
NSData *imageData = UIImagePNGRepresentation(myImage);
PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];
PFObject *pic = [PFObject objectWithClassName:@"Pics"];
//[pic setObject:imageFile forKey:@"Image"];
pic[@"Image"] = imageFile;
PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:@"myPhotos"];
[relation addObject:pic];
[user saveInBackground];
//[pic saveInBackground];
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MainScreen"];
[self presentViewController:myViewController animated:YES completion:nil];
我知道图片本身正在保存,因为当我让图片在后台保存时,当我点击图片时 class 它会显示我拍摄的照片。但是,我的问题是,当我查看 User class 并单击 myPhotos 关系时,没有对象处于该关系中。为什么我的图片没有添加到关系中?
PFObject *details = [PFObject objectWithClassName:@"**your table name**"];
NSData *imageData = UIImagePNGRepresentation(myImage);
PFObject *picDetails = [PFObject objectWithClassName:@"**myphotos**”];
picDetails [@"picture"] = [UIImage imagewithdata:imageData];
[Details save]; [picDetails save];
FRelation *relation = [Details relationForKey:@"**myphotos**"]; [relation addObject:details];
[Details saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
//Do something after the last save...
if (succeeded) {
// The object has been saved.
UIAlertView *success_alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Successfully image saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[success_alert show];
} else {
// There was a problem, check error.description
UIAlertView *fail_alert = [[UIAlertView alloc]initWithTitle:@"Failed" message:@"Failed to save data" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[fail_alert show];
}
}];
This is the way to store in PFRelation using image save. I hope this
answer may help to you. Thank you.
我是解析新手,正在尝试创建一个将用户链接到 his/her 图片的 PFRelation。这是我的代码:
NSData *imageData = UIImagePNGRepresentation(myImage);
PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];
PFObject *pic = [PFObject objectWithClassName:@"Pics"];
//[pic setObject:imageFile forKey:@"Image"];
pic[@"Image"] = imageFile;
PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:@"myPhotos"];
[relation addObject:pic];
[user saveInBackground];
//[pic saveInBackground];
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MainScreen"];
[self presentViewController:myViewController animated:YES completion:nil];
我知道图片本身正在保存,因为当我让图片在后台保存时,当我点击图片时 class 它会显示我拍摄的照片。但是,我的问题是,当我查看 User class 并单击 myPhotos 关系时,没有对象处于该关系中。为什么我的图片没有添加到关系中?
PFObject *details = [PFObject objectWithClassName:@"**your table name**"]; NSData *imageData = UIImagePNGRepresentation(myImage); PFObject *picDetails = [PFObject objectWithClassName:@"**myphotos**”]; picDetails [@"picture"] = [UIImage imagewithdata:imageData]; [Details save]; [picDetails save]; FRelation *relation = [Details relationForKey:@"**myphotos**"]; [relation addObject:details]; [Details saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { //Do something after the last save... if (succeeded) { // The object has been saved. UIAlertView *success_alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Successfully image saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [success_alert show]; } else { // There was a problem, check error.description UIAlertView *fail_alert = [[UIAlertView alloc]initWithTitle:@"Failed" message:@"Failed to save data" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [fail_alert show]; } }];
This is the way to store in PFRelation using image save. I hope this answer may help to you. Thank you.