0

I have a PFObject containing a text and a PFFile.

PFObject *post = [PFObject objectWithClassName:@"Posts"];
post[@"description"] = self.Photodescription.text;
NSData *picture =  UIImageJPEGRepresentation(self.capturedPicture, 0.5f);
post[@"picture"] = [PFFile fileWithName:@"thumbnailPicture.png" data:picture];

I would like to get progress of upload in order to display a progression bar. The following function only works for PFFile.

[post[@"picture"] saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    }progressBlock:^(int percentDone) {
        // Update your progress spinner here. percentDone will be between 0 and 100.
        NSLog(@"%i", percentDone);
    }];

Is there a way to do the same for a PFObject?

Sancho Sanchez
  • 590
  • 1
  • 6
  • 19

1 Answers1

2

Are you uploading a single object or an array of objects? Right now there is no progress for a single PFObject. For very large arrays of PFObjects I did create a category for uploading a series of PFObjects in the background with progress feedback, it works like the normal saveAlInBackground: except you specific the chunkSize (How many PFObjects to save at a time until complete) and give it a progress block handler which is calls each time a chunk is completed:

+(void)saveAllInBackground:(NSArray *)array chunkSize:(int)chunkSize block:(PFBooleanResultBlock)block progressBlock:(PFProgressBlock)progressBlock
{
    unsigned long numberOfCyclesRequired = array.count/chunkSize;
    __block unsigned long count = 0;
    [PFObject saveAllInBackground:array chunkSize:chunkSize block:block trigger:^(BOOL trig) {
        count++;
        progressBlock((int)(100.0*count/numberOfCyclesRequired));
    }];
}

+(void)saveAllInBackground:(NSArray *)array chunkSize:(int)chunkSize block:(PFBooleanResultBlock)block trigger:(void(^)(BOOL trig))trigger
{

    NSRange range = NSMakeRange(0, array.count <= chunkSize ? array.count:chunkSize);
    NSArray *saveArray = [array subarrayWithRange:range];
    NSArray *nextArray = nil;
    if (range.length<array.count) nextArray = [array subarrayWithRange:NSMakeRange(range.length, array.count-range.length)];
    [PFObject saveAllInBackground:saveArray block:^(BOOL succeeded, NSError *error) {
        if(!error && succeeded && nextArray){
            trigger(true);
            [PFObject saveAllInBackground:nextArray chunkSize:chunkSize block:block trigger:trigger];
        }
        else
        {
            trigger(true);
            block(succeeded,error);   
        }
    }];
}
Scott Solmer
  • 3,871
  • 6
  • 44
  • 72
hybrdthry911
  • 1,259
  • 7
  • 12
  • This is a smart solution ! unfortunately I only upload one object, and would like to limit the number of queries as much as possible. – Sancho Sanchez Jun 04 '14 at 09:01