如何使用AFHTTPRequestOperation创建多个帖子请求?

时间:2015-10-29 02:40:53

标签: ios objective-c afnetworking-2 nsoperation nsoperationqueue

我是IOS应用程序的新开发人员。

今天,我正在使用多个帖子请求创建应用程序'我正在使用AFNetworking来创建'发布请求'在我的申请中。

我想在我的应用程序中使用NSOperationQueue进行多次发布请求时出现问题'。在AFNetworking的自述文件中,我获得了良好的代码

NSMutableArray *mutableOperations = [NSMutableArray array];
for (NSURL *fileURL in filesToUpload) {
    NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://localhost/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];
    }];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[mutableOperations addObject:operation];
}

NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:@[...] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
    NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
    NSLog(@"All operations in batch complete");
}];
[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];

但是,此代码不支持我的流程&#39;发布请求&#39;。

这是我的流程&#39;发布请求&#39;在我的申请中。我有2个实体,用户和图像。用户有很多图片。在我发送请求之前&#39;对于用户实体,我想上传图片属于用户和所有“发布请求”#39;当其中一张图片无法上传时,我们会取消 这是我的多个帖子请求的代码&#39;使用NSOperationQueue。

这是我运行流程帖请求的代码:

- (void)postModel{
    NSFetchRequest *fetchRequest    = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity     = [NSEntityDescription entityForName:@"Users" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error                  = nil;
    self.Users                    = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (error) {
        NSLog(@"Unable to execute fetch request.");
        NSLog(@"%@, %@", error, error.localizedDescription);

    } else {
        self.uploadQueue = [NSOperationQueue new];//[[NSOperationQueue alloc] init];
        NSMutableArray *mutableOperations = [NSMutableArray array];
        for (Users *user in self.users) {
            if (user.images) {
                for (Images *image in user.images) {
                    [mutableOperations addObject:[self postImage:image]];
                }
            }
            [mutableOperations addObject:[self postUser:user]];
            for( int index = 0; index < [mutableOperations count]; index = index + 1 )
            {
                AFHTTPRequestOperation *secondOperation    = mutableOperations[index];
                if (index != 0) {
                    AFHTTPRequestOperation *firstOperation     = mutableOperations[index - 1];
                    [secondOperation addDependency:firstOperation];
                }
            }
            NSArray *operations = [NSArray arrayWithArray:mutableOperations];
            [self.uploadQueue addOperations:operations waitUntilFinished:NO];
        }
    }
}

-(AFHTTPRequestOperation *)postImage:(Images *)image{
    NSManagedObjectContext *context     = [self managedObjectContext];
    NSString *urlString                 = @"http://localhost/image/upload";
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            if ([imageType isEqualToString:@"jpg"] || [imageType isEqualToString:@"jpeg"]) {
                [formData appendPartWithFileData:image.picture name:@"image_user" fileName:image.name mimeType:@"image/jpeg"];
            }else{
                [formData appendPartWithFileData:image.picture name:@"image_user" fileName:image.name mimeType:@"image/png"];
    }error:nil];
    [request setTimeoutInterval:600];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Image ID : %@", responseObject[@"id"]);
        NSError *error = nil;
        image.image_id = [NSNumber numberWithInteger: [responseObject[@"id"] integerValue]];
        if (![context save:&error]) {
            NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    return operation;
}

-(AFHTTPRequestOperation *)postUser:(Users *)user{
    NSString *urlString             = @"http://localhost/user/upload";
    NSMutableURLRequest *request    = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFormData:[user.name dataUsingEncoding:NSUTF8StringEncoding]
                                    name:@"name"];
        if (user.images) {
            for (Images *image in user.images) {
                [formData appendPartWithFormData:[[image.image_id stringValue] dataUsingEncoding:NSUTF8StringEncoding]
                                            name:@"images_id[]"];
            }
        }
    }error:nil];
    [request setTimeoutInterval:600];
    operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"USER ID : %@", responseObject[@"user_id"]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    return operation;
} 

我的代码出现问题,我总是从我的XCODE日志中获取错误代码并使我的应用程序崩溃

  

由于未捕获的异常而终止应用   &#39; NSInvalidArgumentException&#39;,原因:&#39; - [_ NSInlineData   objectForKeyedSubscript:]:无法识别的选择器发送到实例   0x7ff06e1e49b0&#39;

当我的第一个帖子请求成功时,我收到此错误。但是,其他发布请求不会发送到我的本地服务器。 这里的任何人都可以帮我解决问题吗?

由于

0 个答案:

没有答案