解析:是否可以跟踪PFObject上传的进度

时间:2014-06-03 08:53:40

标签: objective-c save parse-platform

我有一个包含文本和PFFile的PFObject。

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];

我希望上传进度以显示进度条。以下功能仅适用于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);
    }];

有没有办法为PFObject做同样的事情?

1 个答案:

答案 0 :(得分:2)

您上传的是单个对象还是array个对象?目前单个PFObject没有进展。对于非常大的PFObjects数组,我确实创建了一个类别,用于在后台上传一系列PFObject并获得进度反馈,它的工作方式与普通的saveAlInBackground:类似,除了你特定的chunkSize(一次保存多少PFObjects直到完成并给它一个进度块处理程序,每次完成一个块时调用:

+(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);   
        }
    }];
}