parse-platform - 如何在 Parse.com 离线时将图像 PFFile 保存在本地数据存储中?

标签 parse-platform offline pfuser local-datastore pffile

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

我的应用程序很简单,我有:

  1. 登录VC

  2. 包含所有汽车的表格 View “每个 PFUSER 的汽车”

  3. ViewController“添加汽车”+

我在 Parse.com 文档中搜索,并在离线时找到了 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”。所以它不起作用

所以我采取了另一种方法,当用户登录时(在线时必须登录),我将服务器中的所有值固定到本地数据存储,如下所示:

- (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 中的所有 PFObject 保存并取消固定到服务器(如果没有互联网,则无法注销),如下所示:

-(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 等... ** 这个问题有一个解决方案???

谢谢

最佳答案

当你强制退出应用程序时,AppDelegate 中会调用此方法:

applicationWillTerminate:

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

但是,当应用程序处于非事件状态时间过长时,它就会进入非事件状态。您可以使用应用程序委托(delegate)中的另一个方法来处理这种情况:

applicationWillResignActive:

参见UIApplicationDelegate Protocol了解更多可以处理此类应用程序相关事务的地方。

关于parse-platform - 如何在 Parse.com 离线时将图像 PFFile 保存在本地数据存储中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30213808/

相关文章:

Android 项目提示 "The type bolts.Task cannot be resolved. It is indirectly referenced from required .class files"

javascript - 如何使用 Parse Cloud Code 更新对象的属性?

ios - 重启应用后 PFUser currentUser 返回 nil

ios - PFUser 不在 iOS 上返回 objectID 值

ios - 更新缓存的 PFUser currentUser?

javascript - 解析云 : Remove installation

xcode - 将 PFGeoPoint 转换为字符串错误

"save as"网页及离线浏览的C#工具

javascript - Firebase Web 实时数据库是否具有 PWA 离线功能

ios - 在移动设备上进行离线反向地理编码的技术?