在 For 循环结束时显示 UIAlertView...如果没有错误
Showing UIAlertView at END of a For Loop...if NO errors
我的以下代码循环遍历一个数组,以便 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];
}];
}
}
更新 - 今天早上我似乎总是遗漏重要的细节。 for 循环内部正在执行异步工作,所以显然是行不通的。
因此, promises 提供了一种干净的方法来处理这种情况,您可以在 ios 中使用 promise 实现(例如 promisekit)。
循环的每次迭代都会围绕其上传操作定义一个承诺。您将从所有这些单独的承诺中创建一个复合承诺。复合承诺成功后,您会显示警报。
我建议使用 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.
});
}
我的以下代码循环遍历一个数组,以便 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];
}];
}
}
更新 - 今天早上我似乎总是遗漏重要的细节。 for 循环内部正在执行异步工作,所以显然是行不通的。
因此, promises 提供了一种干净的方法来处理这种情况,您可以在 ios 中使用 promise 实现(例如 promisekit)。
循环的每次迭代都会围绕其上传操作定义一个承诺。您将从所有这些单独的承诺中创建一个复合承诺。复合承诺成功后,您会显示警报。
我建议使用 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.
});
}