说我有一个我正在编辑的PFObject。在稍后阶段,我希望取消我对PFObject所做的更改。如何恢复到PFObject的原始副本?
我尝试了什么
if (self.request.isDirty) { // self.request is a PFObject
// Reload object
NSLog(@"%@", self.request.requestTitle); // Logs ABC, Original was DEF
[self.request refreshInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error) {
NSLog(@"%@", self.request.requestTitle); // Logs ABC
[self.requestDetailsTableView reloadData];
}
}];
}
我也尝试获取对象,但结果相同
if (self.request.isDirty) { // self.request is a PFObject
// Reload object
NSLog(@"%@", self.request.requestTitle); // Logs ABC, Original was DEF
[self.request fetchInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error) {
NSLog(@"%@", self.request.requestTitle); // Logs ABC
[self.requestDetailsTableView reloadData];
}
}];
}
答案 0 :(得分:3)
PFObject
现在有一种还原方法,只要您没有调用save,就会将对象重置为服务器状态:http://parseplatform.org/Parse-SDK-iOS-OSX/api/Classes/PFObject.html#/c:objc(cs)PFObject(im)revert
例如,我使用带有表单的推送视图控制器来编辑我的PFObject子类的属性,并且我有两个取消和保存按钮...
@IBAction override func cancelAction() {
self.myObject?.revert()
self.navigationController?.popViewControllerAnimated(true)
}
@IBAction override func saveAction() {
self.myObject?.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in
if (succeeded) {
self.navigationController?.popViewControllerAnimated(true)
} else {
// Show error
}
})
}
答案 1 :(得分:0)
目前没有办法直接执行此操作 - 您需要在通话时做一些花哨的步法:)
试试这个:
if (self.request.isDirty) { // self.request is a PFObject
// Reload object
NSLog(@"%@", self.request.requestTitle); // Logs ABC, Original was DEF
PFQuery *newRequest = [PFQuery queryWithClassName:@"YourClassName"];
//Add your request parameters here...
[newRequest getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error)
self.request = object;
NSLog(@"%@", self.request.requestTitle); // Should log DEF
[self.requestDetailsTableView reloadData];
}
}];
}