GithubHelp home page GithubHelp logo

mobilevet / pffilesaveeventually Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thibauddavid/pffilesaveeventually

0.0 2.0 0.0 184 KB

A sample class to manage offline saving of PFFile

Objective-C 99.15% Ruby 0.85%

pffilesaveeventually's Introduction

PFFileEventuallySaver

This is a sample class to save a PFFile eventually.

As you might know, that feature isn't available within ParseSDK.

That's only a working PoC with limitations such as only working for a single Parse class to associate saved PFFile on.

It requires Reachability pod 'Reachability', '~> 3.2'

How to use it ? Well, I guess the sample projects describes it well, but here is a piece of code to understand how it works :

(Remember to run pod install to resolve dependencies before running example)

/*
 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);
}];

This could be greatly improved, but I thought you guys might found that useful, as I would've wanted to find a similar working example.

pffilesaveeventually's People

Contributors

thibauddavid avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.