AFNetworking:使用AFJSONRequestOperation处理异步获取请求

时间:2013-06-26 03:38:55

标签: ios objective-c afnetworking mbprogresshud afjsonrequestoperation

我在类(静态方法)中有以下代码,我调用它来从API获取数据。我决定将它作为静态方法,以便我可以在应用程序的其他部分重用它。

+ (NSArray*) getAllRoomsWithEventId:(NSNumber *)eventId{

    NSURL *urlRequest = [NSURL URLWithString:[NSString stringWithFormat:@"http://blablba.com/api/Rooms/GetAll/e/%@/r?%@", eventId, [ServiceRequest getAuth]]];

    NSMutableArray *rooms = [[NSMutableArray alloc] init];

    NSURLRequest *request = [NSURLRequest requestWithURL:urlRequest];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSLog(@"Response of getall rooms %@", JSON);


        NSArray *jsonResults = (NSArray*)JSON;
        for(id item in jsonResults){
            Room* room = [[Room alloc]init];
            if([item isKindOfClass:[NSDictionary class]]){
                room.Id = [item objectForKey:@"Id"];
                room.eventId = [item objectForKey:@"EventId"];
                room.UINumber = [item objectForKey:@"RoomUIID"];
                [rooms addObject:room];
            }
        }
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
        NSLog(@"Error");
     }];

    [operation start];
    [operation waitUntilFinished];

    return rooms;
}

现在我的问题是,每当我在ViewController(ViewDidLoad方法)中调用它时。静态方法将一直运行到最后,并将在房间返回null,但Nslog将在几秒后显示“成功”块Nslog。现在我明白这是异步的,因此它不会等到成功块在它到达“返回房间”之前执行。线。总而言之,我需要一些关于如何处理这个的建议,比如可能是进度条或类似的东西?或者延迟它的事情?我不确定这是否是正确的方式,或者如果是,我不知道该怎么做。

非常感谢任何建议。谢谢!

2 个答案:

答案 0 :(得分:3)

AFNetworking围绕异步性 - 启动请求,然后在请求完成后执行一些代码构建。

waitUntilFinished是一种反模式,可以阻止用户界面。

相反,您的方法应该没有返回类型(void),并且有一个完成块参数,它返回序列化的房间数组:

- (void)allRoomsWithEventId:(NSNumber *)eventId block:(void (^)(NSArray *rooms))block { // ... }

有关如何执行此操作的示例,请参阅AFNetworking项目中的示例应用程序。

答案 1 :(得分:-1)

您可以按照以下方式编写方法:

+ (void) getAllRoomsWithEventId:(NSNumber *)eventId:(void(^)(NSArray *roomArray)) block
{

    NSURL *urlRequest = [NSURL URLWithString:[NSString stringWithFormat:@"http://blablba.com/api/Rooms/GetAll/e/%@/r?%@", eventId, [ServiceRequest getAuth]]];

    NSMutableArray *rooms = [[NSMutableArray alloc] init];

    NSURLRequest *request = [NSURLRequest requestWithURL:urlRequest];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSLog(@"Response of getall rooms %@", JSON);


        NSArray *jsonResults = (NSArray*)JSON;
        for(id item in jsonResults){
            Room* room = [[Room alloc]init];
            if([item isKindOfClass:[NSDictionary class]]){
                room.Id = [item objectForKey:@"Id"];
                room.eventId = [item objectForKey:@"EventId"];
                room.UINumber = [item objectForKey:@"RoomUIID"];
                [rooms addObject:room];
            }
        }
        block(rooms);
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
        NSLog(@"Error");
        block(nil); //or any other error message..
     }];

    [operation start];
    [operation waitUntilFinished];

}

你可以像下面这样称呼这个方法:

[MyDataClass getAllRoomsWithEventId:@"eventid1":^(NSArray *roomArray) {
      NSLog(@"roomArr == %@",roomArray);
    }];