如何在Offline Parse.com中将图像PFFile保存在localdatastore中?

时间:2015-05-13 11:43:01

标签: parse-platform offline pfuser local-datastore pffile

我正在开发一个使用Parse.com服务器为每个PFUser存储PFUsers信息和类的应用程序。当我在线时,我可以将所有内容保存到服务器,但是当没有互联网连接时我也想使用该应用程序,这意味着当我离线时也可以保存它们。

我的应用很简单,我有:

  1. LoginVC

  2. Tableview与所有汽车“汽车为每个PFUSER”

  3. ViewController“添加汽车”+

  4. 我在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而我无法在本地数据库中恢复图像,剩下的就是”字符串“即:飞机的名称等...... **这个问题有一个解决方案???

    谢谢

2 个答案:

答案 0 :(得分:1)

强制退出应用时,会在AppDelegate中调用此方法:

applicationWillTerminate:

您可以在那里注销您的用户。

但是,当应用程序处于非活动状态的时间过长时,它会进入非活动状态。你可以使用app delegate中的另一个方法来处理这种情况:

applicationWillResignActive:

有关您可以处理此类应用相关事项的更多地方,请参阅UIApplicationDelegate Protocol

答案 1 :(得分:1)

不幸的是,我认为PFFiles不会保留在本地数据存储区中。请参阅:related question