ios - 带有渐进式 UIAlertView 的 AFNetworking 下载

标签 ios iphone objective-c afnetworking uialertview

我想实现一个 UIAlertView,它将显示 [[AFHTTPRequestOperationManager manager] GET:] 何时开始执行其工作,并在工作完成后自动消失。一个很酷的额外功能是,如果我可以显示 UIProgressView 以及 AFHTTPRequestOperation 的进度。

现在我正在检查 Core Data 中是否有任何内容,并基于此初始化 UIAlertView:

if (![self coreDataHasEntriesForEntityName:@"Group"]) {
        downloadingAlert = [[UIAlertView alloc] initWithTitle:@"Pobieranie" message:@"Trwa pobieranie grup" delegate:nil cancelButtonTitle:@"Anuluj" otherButtonTitles:nil, nil];
        [self collectData];
    } else {
        NSError *error;
        [[self fetchedResultsController] performFetch:&error];
    }

因此,为了防止在 UITableView 已填充数据时显示此警报。 如您所见,我正在调用这个 [self collectData] 方法,它看起来像:

-(void)collectData
{
    [downloadingAlert show];

    [[AFHTTPRequestOperationManager manager] GET:ALL_GROUPS parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSManagedObjectContext *tempContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
        tempContext.parentContext = self.moc;

        [tempContext performBlock:^{

            // Doing something with responseObject

            if (![tempContext save:&error]) {
                NSLog(@"Couldn't save: %@", [error localizedDescription]);
            }

            [self.moc performBlock:^{
                NSError *error;
                if (![self.moc save:&error]) {
                    NSLog(@"Couldn't save: %@", [error localizedDescription]);
                }

                [downloadingAlert dismissWithClickedButtonIndex:0 animated:YES];

                [self.writer performBlockAndWait:^{
                    NSError *error;
                    if (![self.writer save:&error]) {
                        NSLog(@"Couldn't save: %@", [error localizedDescription]);
                    }
                }];

            }];
        }];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // Show alert with info about failure
    }];
}

如您所见,我以编程方式显示此 UIAlertView 并在下载完成并重新加载 UITableView 时关闭它。但我不知道如何添加 UIProgressView 或如何在不使用 dismissWithClickedButtonIndex: 的情况下关闭此 UIAlertView。有什么想法吗?

最佳答案

AFNetworking 2.0 HTTP POST Progress

如果您将使用以下方法,您可以收到进度信息

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
                                         fromData:(NSData *)bodyData
                                         progress:(NSProgress * __autoreleasing *)progress
                                completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler

AFHTTPSessionManager 类

关于ios - 带有渐进式 UIAlertView 的 AFNetworking 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22133405/

相关文章:

ios - 在设计方面,设计 UIPageViewController 布局以便多次重用的最佳方法是什么?

iphone - NSUserDefaults - 存储和检索数据

iphone - PhoneGap 为 iPhone 捕获 'Go' 按下事件

ios - watchkit 中的 LongPressGestureRecognizer

Objective-C 导入

iphone - 使用 JSON 问题在 UITableView 中导航缓慢

ios - 创建 NSManagedObject 类时,使用 Coredata 的项目中出现 Apple Mach-O 链接器错误

ios - iPhone上的验证码。

iphone - 如何在 ios 推送通知出现之前对其进行过滤

iOS Objective-C : convert utf-16 to base64