我正在开发一个使用 Parse.com 服务器为每个 PFUser 存储 PFUser 信息和类的应用程序。当我在线时,我可以将所有内容保存到服务器,但我也想在没有互联网连接时使用该应用程序,这意味着当我离线时也可以保存它们。
我的应用程序很简单,我有:
登录VC
包含所有汽车的表格视图“每个 PFUSER 的汽车”
ViewController "添加汽车" +
我在 Parse.com Docs 中搜索,当我们离线时我发现 SaveEventually。
- (IBAction)save:(id)sender {
PFUser*user = [PFUser currentUser];
PFObject *CarsObject = [PFObject objectWithClassName:@"Cars"];
CarsObject[@"makeandmodel"] = self.MakeModelTextfield.text;
CarsObject[@"registrationnumber"] = self.CarImmatriculation.text;
CarsObject[@"typeofCar"] = TypeACString;
CarsObject[@"categoryofCar"] = CategoryString;
CarsObject[@"User"] = user;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeDeterminate;
hud.animationType = MBProgressHUDAnimationZoom;
hud.labelText = @"Uploading";
[hud show:YES];
NSData *imageData = UIImageJPEGRepresentation(CarsImageView.image, 0.5);
NSString *filename = [NSString stringWithFormat:@"%@.jpg",self.CarsImmatriculation.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[CarsObject setObject:imageFile forKey:@"CarImage"];
[CarObject SaveEventually:^(BOOL success, NSError*error){
[hud hide:YES];
if (!error) {
[self dismissViewControllerAnimated:YES completion:nil];
}else {
NSLog(@"ERROR SAVE");
}
}];
}
使用上面的代码我得到这个错误[错误]:捕获“NSInternalInconsistencyException”,原因是“无法最终保存一个与新的、未保存的 PFFile 相关的 PFObject”。所以它不起作用
所以我做了另一种方法,当用户登录时(当你在线时必须登录),我将服务器中的所有值固定到 localdatastore,如下所示:
- (void)FetchFromServerAtLogin{
PFQuery*query = [PFQuery queryWithClassName:@"Cars"];
[query whereKey:@"User" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray*object, NSError*error){
if (!error) {
[PFObject pinAllInBackground:object block:nil];
}else{
NSLog(@"ERROR in Fetch From Server At login");
}
}];
}
我有一个 Tableview 显示所有汽车并从 Localdatastore 显示它们,因此它可以与以下代码一起使用:
- (void)FetchFromDatabase{
[self ShowAircraftCategory];
PFQuery*query = [PFQuery queryWithClassName:@"Cars"];
[query fromLocalDatastore];
[query whereKey:@"User" equalTo:[PFUser currentUser]];
[query whereKey:@"categoryofCars" equalTo:self.CategoryACString];
[query findObjectsInBackgroundWithBlock:^(NSArray*object, NSError*error){
if (!error) {
NSArray*temp = [NSArray arrayWithArray:object];
self.CarsArray = [temp mutableCopy];
[self.tableView reloadData];
}else{
NSLog(@"ERROR in FetchFromDatabse");
}
}];
}
而且它有效所以此时我可以使用以下代码获取我从 VC 创建的所有汽车:
- (IBAction)save:(id)sender {
PFUser*user = [PFUser currentUser];
PFObject *CarsObject = [PFObject objectWithClassName:@"Cars"];
CarsObject[@"makeandmodel"] = self.MakeModelTextfield.text;
CarsObject[@"registrationnumber"] = self.CarImmatriculation.text;
CarsObject[@"typeofcar"] = TypeACString;
AircraftObject[@"categoryofcar"] = CategoryString;
AircraftObject[@"User"] = user;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeDeterminate;
hud.animationType = MBProgressHUDAnimationZoom;
hud.labelText = @"Uploading";
[hud show:YES];
NSData *imageData = UIImageJPEGRepresentation(CarImageView.image, 0.5);
NSString *filename = [NSString stringWithFormat:@"%@.jpg",self.CarImmatriculation.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[CarsObject setObject:imageFile forKey:@"AircraftImage"];
[CarsObject pinInBackgroundWithBlock:^(BOOL success, NSError*error){
[hud hide:YES];
if (!error) {
[self dismissViewControllerAnimated:YES completion:nil];
}else {
NSLog(@"ERROR SAVE");
}
}];
}
我发现将 Localdatastore 保存到服务器的最后一部分和唯一方法:使用“LOGOUT”按钮将 localDatastore 中的所有 PFObjects 保存并取消固定到服务器(如果没有互联网,则无法注销)作为跟随:
-(IBAction)Logout:(id)sender{
PFQuery*query = [PFQuery queryWithClassName:@"Cars"];
[query fromLocalDatastore];
[query whereKey:@"User" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray*arrayOb, NSError*error){
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
hud.animationType = MBProgressHUDAnimationFade;
hud.labelText = @"Uploading";
[hud show:YES];
if (error == nil) {
NSArray*Cars = [NSArray arrayWithArray:arrayOb];
[PFObject saveAllInBackground:Cars block:^(BOOL succeeded, NSError *error){
[hud hide:YES];
if (error == nil) {
[PFObject unpinAllObjectsInBackgroundWithBlock:nil];
[PFUser logOut];
[self performSegueWithIdentifier:@"Logout" sender:self];
}
else{
UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@"LOGOUT ERROR !" message:@"\n Please Connect to The Internet to SYNC with the Server all What you saved Locally ! " delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}];
}else{
UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@"LOGOUT ERROR !" message:@"\n Please Connect to The Internet to SYNC with the Server all What you saved Locally ! " delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}];
}
**我的问题是“如果我在离线时退出应用程序而不保存,我会丢失 ImageofCar 的 PFFile,并且我无法检索本地数据存储中的图像,剩下的就是“字符串”,即:NameofAircraft等... **这个问题有解决方案???
谢谢