我正在开发一个使用parse.com服务器为每个Pfuser存储pfusers信息和类的应用程序。当我在线时,我可以将所有内容都保存到服务器中,但是当没有Internet连接时,我也想使用该应用程序,这意味着在离线时也可以保存它们。
我的应用很简单,我有:
-
loginvc
-
tableview带有所有汽车的"每个Pfuser的汽车"
-
viewController"添加汽车"
我在parse.com docs&当我们离线时,我发现了拯救。
- (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");
}
}];
}
使用上面的代码,我会得到此错误[错误]:以"无法与新的,未保存的pffile相关的pfobject,抓住了" nsinternalinconsistencyException"。所以它不起作用
所以我采取了另一种方法,当用户登录时(在线登录时必须登录),我将所有值从服务器固定到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保存到服务器的独特方法:与按钮"注销"保存,并在LocalDatastore中的所有pfobject保存到服务器(如果没有Internet,则无法注销):
:-(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,我无法在LocalDatastore中检索图像,那么剩下的就是"字符串"即:NameFaircraft Ect ... **有一个解决此问题的解决方案?
谢谢
当您强制退出应用时,该方法在AppDelegate中被调用:
applicationWillTerminate:
您可以在那里注销用户。
但是,当应用程序无活动时间太长时,它将进入非活动状态。您只需使用应用程序委托中的另一种方法处理该案例:
applicationWillResignActive:
有关您可以处理与应用相关的事物的更多地方,请参见UiapplicationDelegate协议。
不幸的是,我相信pffiles在本地数据存储中没有持续。请参阅:相关问题