3

我在一个类(静态方法)中有以下代码,我调用它来从 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。现在我知道这是异步的,因此它不会等待成功块在到达“返回房间”之前执行;线。尽管如此,我需要一些关于如何处理这个问题的建议,比如进度条或类似的东西?还是有什么耽误的?我不确定这是否是正确的方式,或者如果是,我不知道该怎么做。

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

4

2 回答 2

3

AFNetworking 是围绕异步构建的——启动一个请求,然后在该请求完成后执行一些代码。

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

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

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

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

于 2013-06-26T16:10:48.263 回答
-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);
    }];
于 2013-06-26T04:15:48.717 回答