I recently asked about the null value from an AFNetwirking GET request. Here is the question
I have 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 have a method called 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 a non-empty value (NSArray places). And now he again raises the question of asynchronous tasks. What should I do? I want to access from another class using NSArray *places = [MyClass getAllPlaces] I hope to get the correct answer. Thanks, Artem.
source share