解析:是否可以跟踪PFObject上传的进度 [英] Parse : is it possible to follow progress of PFObject upload

查看:75
本文介绍了解析:是否可以跟踪PFObject上传的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想获取上传进度,以便显示进度条.以下功能仅适用于PFFile.

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

是否可以对PFObject进行相同的操作?

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

推荐答案

您要上传单个对象还是对象的array?现在,单个PFObject没有任何进展.对于很大的PFObjects数组,我确实创建了一个类别,用于在后台上传一系列PFObjects,并提供进度反馈,它的工作原理与普通的saveAlInBackground:相同,只是您指定了chunkSize(一次完成要保存多少个PFObjects)和给它一个进度块处理程序,每次块完成时调用该处理程序:

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

这篇关于解析:是否可以跟踪PFObject上传的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆