gpt4 book ai didi

ios - 在 For 循环结束时显示 UIAlertView...如果没有错误

转载 作者:行者123 更新时间:2023-11-29 00:36:46 25 4
gpt4 key购买 nike

我的以下代码循环遍历 Parse.com 的数组,以从本地数据存储上传到服务器。当然,这里的问题是,它为每个成功的条目创建一个 UIAlertView...这很快就会变得烦人。我怎样才能改变它,以便它仍然只在没有错误的情况下显示,但只在循环结束时执行一次?

-(void)uploadToParse {

int i;
for (i = 0; i < [self.objects count]; i++) {
PFObject *entry = [self.objects objectAtIndex:i];


NSString *imageFileName = [entry[@"FamilyName"] stringByAppendingString:@".png"];
NSLog(@"%@", imageFileName);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:imageFileName];
UIImage *newImage = [UIImage imageWithContentsOfFile:filePath];
NSData* data = UIImagePNGRepresentation(newImage);


PFFile *imageFile = [PFFile fileWithName:@"Locationimage.png" data:data];

[entry setObject:imageFile forKey:@"HousePicture"];


[entry saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
if (!error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Complete" message:@"Successfully added your Bible Distribution data." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];


[entry unpinInBackgroundWithName:@"Share" block:^(BOOL succeeded, NSError * _Nullable error) {

}];

}
}];


[entry unpinInBackgroundWithName:@"Share" block:^(BOOL succeeded, NSError * _Nullable error) {
[self loadObjects];
}];

}
}

最佳答案

我建议使用 dispatch_group。它们允许您对任务进行排队,并且只有在每个任务完成后才会调用完成处理程序。

这应该有效:

-(void)uploadToParse {

dispatch_group_t uploadGroup = dispatch_group_create();
int i;
for (i = 0; i < [self.objects count]; i++) {

dispatch_group_enter(uploadGroup);
// ... Your code that sets up the upload task


[entry saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
dispatch_group_leave(uploadGroup);
}];
}

dispatch_group_notify(uploadGroup, dispatch_get_main_queue(),^{
// This gets called after each task completes.
// Add code to show alert view here.
});
}

关于ios - 在 For 循环结束时显示 UIAlertView...如果没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40407519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com