在IOS块方法中读取多个数据 [英] Read multiple data in block method IOS

查看:53
本文介绍了在IOS块方法中读取多个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以这种方式从数据库下载一系列图像:

I am trying to download a series of images from the database in this way:

- (void)dowloadAdditionalFileDataForFileObjectId:(NSString *)assignedObjectId andUserID:(NSString *)userID completition:(void (^) (BOOL success))block {
if (userID != nil && assignedObjectId != nil) {
    PFQuery *query = [PFQuery queryWithClassName:@"AdditionalFileData"];
    [query whereKey:@"fileAssignedObjectId" equalTo:assignedObjectId];
    [query whereKey:@"userID" equalTo:userID];

    [query findObjectsInBackgroundWithBlock: ^(NSArray *objects, NSError *error) {
        if (!error) {
            PFObject *object = [objects firstObject];

            dispatch_group_t group = dispatch_group_create();
            dispatch_group_enter(group);

            __block  UIImage *image1;
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            if (object[@"image1"] != nil)
                [self downloadDataForObject:object withParameter:@"image1" andCallback:^(NSData *data) {
                    image1 = [UIImage imageWithData:data];
                    dispatch_group_leave(group);
                }];
            });

            __block  UIImage *image2;
            if (object[@"image2"] != nil)
            [self downloadDataForObject:object withParameter:@"image2" andCallback:^(NSData *data) {
                image2 = [UIImage imageWithData:data];
            }];

            dispatch_group_wait(group,  DISPATCH_TIME_FOREVER);

            [DBManager storeAdditionalDataForFileWithObjectId:assignedObjectId forUserID:userID withImage1:image1 andImage2:image2 andImage3:image3 ...] local:NO completition: ^(BOOL finished) {
                if (finished)
                    block(YES);
                else
                    block(NO);
            }];
        }
        else
            block(NO);
    }];
}
else
    block(NO);
}

- (void)downloadDataForObject:(PFObject *)object withParameter:(NSString *)parameter andCallback:(void (^)(NSData *data))callback{
[object[[NSString stringWithFormat:@"%@", parameter]] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
    callback(data);
}];
}

如果我只是写:

image1 = [UIImage imageWithData:[... getData]];

然后一切正常,但是在该过程结束之前,用户界面一直处于阻塞状态.
我想要一些系统,以便在后台下载数据.由于我的图像可能多于2张,因此嵌套块将无法解决任何问题.
我尝试使用上面看到的代码.

then everything works fine, however the user interface is blocked until the process is over.
I want some system such that the data is downloaded in the background. Since I might have more images than 2, nesting the blocks would't solve anything.
I tried using the code you see above.

有什么建议吗?

忘记明确提及所有下载方法,例如

Forgot to mention explicitly that all of the download methods such as

getDataInBackground

getDataInBackground

调用Parse SDK( Parse.com ).

call the Parse SDK (Parse.com).

推荐答案

您可以为此使用螺栓库,该库已经包含解析.将使代码简单得多,下面的示例可以轻松扩展为2个以上的图像或其他文件.

You could use the bolts library for this, which parse already includes. Would make the code a lot simpler, the example below could be easily extended for more than 2 images or other files.

    // do your query
    PFObject *object = [objects firstObject];
    PFFile *file1 = object[@"image1"];
    PFFile *file2 = object[@"image2"];
    BFTask* task1 = [file1 getDataInBackground];
    BFTask* task2 = [file2 getDataInBackground];
    [BFTask taskForCompletionOfAllTasks:@[task1, task2]] continueWithBlock:^id(BFTask *task) {
        if(!task.error){
            UIImage* image1 = [UIImage imageWithData:task1.result];
            UIImage* image2 = [UIImage imageWithData:task2.result];
            // do your thing with the images
        }
        return nil;
    }];

这篇关于在IOS块方法中读取多个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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