【问题标题】:Saving PFFile Eventually最终保存 PFFile
【发布时间】:2015-09-10 09:07:33
【问题描述】:

当网络可达时,PFObject 最终可以保存在 Parse 上同步,同时保持在本地。

如果您的文件包含 PFFile,则必须先将文件 saveInBackground,然后才能保存 PFObject。

如何最终保存一个 PFFile,以便现在发送,或者稍后在网络可达时发送?

【问题讨论】:

    标签: ios parse-platform pffile


    【解决方案1】:

    您可能知道,ParseSDK 中没有该功能,因此在看到一些模糊解释如何绕过此功能的帖子后,我编写了一个示例工作 XCode 项目

    这只是一个有效的 PoC,有一些限制,例如仅适用于单个 Parse 类来关联保存的 PFFile。

    它需要 Reachability pod 'Reachability', '~> 3.2'

    如何使用它?好吧,我想示例项目描述得很好,但这里有一段代码可以理解它是如何工作的:

    (记得在运行示例之前运行 pod install 以解决依赖关系)

    /*
     This example uses an UIImage, but this works with any file writable as NSData
     We begin by writing this image in our tmp directory with an uuid as name.
     */
    UIImage *nyancat = [UIImage imageNamed:@"nyancat.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(nyancat, 0.5);
    
    NSString *filename = [[NSUUID UUID] UUIDString];
    NSURL *fileUrl = [PFFileEventuallySaver fileURLInTmpWithName:filename];
    
    [imageData writeToURL:fileUrl atomically:YES];
    
    
    /*
     We create a PFObject (you can pass an array to below function if you need your file to be saved on several objects). If upload works on first time, do what you want with your file, like linking it on your PFobject.
    
     If saving fails, it'll be retried as soon as network is available, on this session or nexts launches of app.
     In that case, the pointer at key kPFFILE_MANAGER_OBJECT_FILE_KEY of your PFFObject will be set with the PFFile, then saved eventually within PFFileEventuallySaver
     */
    PFObject *object = [PFObject objectWithClassName:kPFFILE_CONTAINER_OBJECT_CLASSNAME];
    
    [[PFFileEventuallySaver getInstance] trySaveobjectAtURL:fileUrl associatedObjects:@[object] withBlock:^(PFFile *file, NSError *error) {
        if(!error)
        {
            NSLog(@"[First try, network is fine] File saved, saving PFObject");
    
            object[kPFFILE_MANAGER_OBJECT_FILE_KEY] = file;
            [object saveEventually];
    
            NSLog(@"Try again disabling your network connection");
        }
        else
        {
            NSLog(@"No network, connect back your wifi, or relaunch app. Your file will be sent");
        }
    } progressBlock:^(int percentDone) {
        NSLog(@"[First try, network is fine] Sending file %d/100%%", percentDone);
    }];
    

    这可以大大改进,但我认为你们可能会发现这很有用,因为我想找到一个类似的工作示例。

    【讨论】:

      猜你喜欢
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 2021-05-11
      • 2015-07-24
      • 2023-04-02
      • 1970-01-01
      • 2022-01-11
      相关资源
      最近更新 更多