Save eventually on PFObject with PFFile (Parse Local Datastore)?

半城伤御伤魂 提交于 2019-11-28 06:35:42

PFFiles still don't support saveEventually see here

That page was last updated : 2015-01-23

You could pinInBackgroundWithBlock and if successful save the PFFile to a temporary folder in you app bundle and delete it when necessary or unpinned

I just released a class which allows to saveEventually a PFFile.

You can find it here :

/*
     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);
}];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!