简体   繁体   English

AFNetworking从异步任务iOS返回值

[英]AFNetworking Return value from async task iOS

I recently have asked about nil value from AFNetwirking GET query. 我最近从AFNetwirking GET查询中询问了零值。 Here is the question 这是问题

I've got an answer 我有一个答案

+ (void)getRequestFromUrl:(NSString *)requestUrl withCompletion:((void (^)(NSString *result))completion 
{
    NSString * completeRequestUrl = [NSString stringWithFormat:@"%@%@", BASE_URL, requestUrl];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:completeRequestUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *results = [NSString stringWithFormat:@"%@", responseObject];
        if (completion)
            completion(results);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSString *results = [NSString stringWithFormat:@"Error"];
        if (completion)
            completion(results);
    }];
}

[YourClass getRequestFromUrl:@"http://www.example.com" withCompletion:^(NSString *results){
    NSLog(@"Results: %@", results);
}

Now I have another question: in MyClass I've got a method called getAllPlaces. 现在我有另一个问题:在MyClass中,我有一个名为getAllPlaces的方法。

+ (void) getAllPlaces {
    NSString * placesUrl = [NSString stringWithFormat: @"/url"];
    NSMutableArray *places = [[NSMutableArray alloc] init];
    [RestfulActions getRequestFromUrl:cafesUrl withCompletion:^(id placesInJSONFormat) {
        NSArray *results = placesInJSONFormat;
        for (NSDictionary *tempPlaceDictionary in results) {
            Place *place = [[Place alloc] init];
            for (NSString *key in tempPlaceDictionary) {
                if ([place respondsToSelector:NSSelectorFromString(key)]) {
                    [place setValue:[tempPlaceDictionary valueForKey:key] forKey:key];
                }
            }
            [places addObject:place];
        }

}];

It works, but I want to return non-void value (NSArray places). 它有效,但是我想返回非空值(NSArray位置)。 And now it raises question about async tasks again. 现在,它又引发了有关异步任务的问题。 What should I do? 我该怎么办? I want to access from another class with NSArray *places = [MyClass getAllPlaces] I hope to get a correct answer. 我想使用NSArray *places = [MyClass getAllPlaces]从另一个类访问NSArray *places = [MyClass getAllPlaces]我希望获得正确的答案。 Thx, Artem. Thx,Artem。

You can't return an value from that block, as it's asynchronous and continues to run while other tasks are going on. 您无法从该块返回值,因为它是异步的,并且在其他任务正在进行时仍会继续运行。

The correct way to handle this is to call the method that deals with this data from within the block and pass it the array. 处理此问题的正确方法是从块中调用处理此数据的方法,并将其传递给数组。

You should try to implement the method like this: 您应该尝试实现以下方法:

+ (void) getTimeline:(NSString*)val success:(void (^)(id))success failure:(void (^)(NSError *))failure;

And use it like this: 并像这样使用它:

[MyClass getTimeline:nil
               success:^(id result) {} failure:^(NSError *error) {} 

The success block will have the returned value if any! 成功块将具有返回值(如果有)!

Anything more? 更多的东西? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM