我试图以这种方式从数据库下载一系列图像:
- (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更多的图像,嵌套块不会解决任何问题。
我尝试使用您在上面看到的代码。
有什么建议吗?
编辑:
忘了明确提到所有的下载方法,如
getDataInBackground
调用Parse SDK(Parse.com)。
答案 0 :(得分:3)
你可以使用bolt库来解析已经包含的。将使代码更简单,下面的示例可以轻松扩展为2个以上的图像或其他文件。
// 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;
}];
答案 1 :(得分:2)
虽然我认为您只需要在PFQuery上使用“includeKey”方法,但完整查询应该看起来更像(参见includeKey部分):
@interface MyParseObject:PFObject<PFSubclassing>
@property (retain) NSArray *images;
@end
@implementation MyParseObject
@dynamic images;
int completed;
void (^loadingImagesComplete)(BOOL success);
- (void)loadImagesWithItemCompletion:(void(^)(UIImage *relatedImage))imageCompleted andCompletion:(void(^)(BOOL success))processCompleted{
loadingImagesComplete = processCompleted;
completed = 0;
PFQuery *queryForItems = [PFQuery queryWithClassName:@"yourClassName"];
[queryForItems whereKey:@"yourKey" equalTo:@"yourValue"];
//This is the part I THINK YOURE LOOKING FOR
//THE QUERY IN YOUR QUESTION WOULD INCLUDE
//**[query includeKey:@"image1"]**;
//**[query includeKey:@"image2"]**;
[queryForItems includeKey:@"images"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for(PFFile *imageFile in self.images){
if (!imageFile.isDataAvailable){
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
imageCompleted([UIImage imageWithData:data]); //maybe callback to main queue for this
completed++;
[self checkLoadingCompleted];
}];
}else{
imageCompleted([UIImage imageWithData:[imageFile getData]]);
completed++;
[self checkLoadingCompleted];
}
}
});
}
- (void)checkLoadingCompleted{
if(completed == self.images.count){
loadingImagesComplete(YES);// the block called here should make reference to MainQueue when updating any UI Elements
}
}
@end
@implementation SomeOtherClass
- (void)loadImages{
[self.myParseObject loadImagesWithItemCompletion:^(UIImage *relatedImage) {
//this will fire every time an image is done downloading
} andCompletion:^(BOOL success) {
//this will fire after all images are done downloading
}];
}
@end
答案 2 :(得分:1)
其他数据解决方案:
如果您需要,可以使用下载管理器复制一些SDWebimages行为。另一种选择是AFNetworking,它可以帮助您以更简单的方式异步获取文件,并声明具有缓存。它们都可以在GitHub上找到。
仅图片解决方案:
你可以使用像SDWebImages这样的库来做到这一点。该库基本上为UIImageView提供了一个类别,支持来自Web的远程图像。所有下载都是在后台完成的。它还保证由于本地缓存,您不会一遍又一遍地下载相同的图像。
答案 3 :(得分:1)
[UIImage imageWithData:]
这将阻止直到完成:它不是异步操作。
你似乎已经陷入了一些混乱,试图解决这个问题并且你的代码因此而变得非常复杂,这可能对你没什么帮助。
如果getDataInBackgroundWithBlock
正在按照我的想象行事,它会在数据可用时立即调用完成块。删除调度组并使用回调:让它们在共享方法中更新状态,并且一旦所有数据都可用(即所有块都被触发)调用
storeAdditionalDataForFileWithObjectId
当您知道时,数据可用。
答案 4 :(得分:1)
使用名为Superman的优秀Github库。
#import "Superman.h"
然后使用以下方法。
Superman *downloadImages = [[Superman alloc] init];
[downloadImages setDownloads: @"http://...../image1.png", @"http://...../image2.png", @"http://...../image3.png", @"http://...../image4.png", nil];