Как вызвать многократное повторение api один за другим в цикле
Я пытаюсь получить данные с сервера, чтобы хранить локально, когда это необходимо. Данные присутствуют на разных страницах. Я хочу вызвать все эти apis при одном щелчке обновления. Помогите мне решить эту проблему.
-(void) call:(NSString *)url{ NSURLRequest *Request1 = [NSURLRequest requestWithURL :[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0]; // calling only one api NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:Request1 delegate:self startImmediately:YES]; // flag = true; [SVProgressHUD showWithStatus:@"Wait while we fetch data .."]; if (connection){ NSLog(@"connection success"); } else{ // handle error here } }
- AFNetworking AFHTTPRequestOperation блок никогда не вызывается
- «DataTaskWithRequest» запрашивает NSMutableRequest дважды, когда требуется только один запрос
- отменить NSURLConnection в ios
- Разница между установкой заголовка NSMutableURLRequest и добавлением одного
- Error Domain = NSCocoaErrorDomain Code = 3840 «Операция не может быть выполнена (ошибка Cocoa 3840.)
- NSMutableURLRequest с параметром - не может анализировать ответ
- NSURLSessionDataTask время ожидания последующих запросов не работает
- NSURLCache - кэширование диска для запроса GET с неработающими параметрами
- Как просто протестировать локальное подключение Wifi к IP, например 192.168.0.100 со статусом кода?
- NSMutableURLRequest «Задержка с запросом». ,
- Можно ли загрузить файл на сервер без преобразования его в NSData?
- Установите NSDictionary в HTTPBody и отправьте с помощью метода POST
- Команда Curl для NSURLRequest Swift
One Solution collect form web for “Как вызвать многократное повторение api один за другим в цикле”
Попробуй это,
**.h** @property (nonatomic, strong) NSArray *arrURLs; **.m** - (void)viewDidLoad { [super viewDidLoad]; self.arrURLs = [[NSArray alloc]initWithObjects:@"URL1",@"URL2",@"URL3", nil]; NSTimer *timer; timer = [NSTimer scheduledTimerWithTimeInterval: 5 target: self selector: @selector(fetchContent) userInfo: nil repeats: YES]; } -(void)fetchContent { [self fetchContentOnebyOne:0]; } - (void)fetchContentOnebyOne:(int)index { __block int cIndex = index; if( index >= self.arrURLs.count ) return; NSString *strURL = [self.arrURLs objectAtIndex:index]; [self fetchfromServer:strURL withCompletion:^(BOOL isSuccessful) { if (isSuccessful) { cIndex++; [self fetchContentOnebyOne:cIndex]; } else { cIndex++; [self fetchContentOnebyOne:cIndex]; } if (cIndex == self.arrURLs.count) { NSLog(@"updated all content...."); } }]; } - (void)fetchfromServer:(NSString*)strURL withCompletion:(void (^)(BOOL isSuccessful))completion { NSURLRequest *Request1 = [NSURLRequest requestWithURL :[NSURL URLWithString:strURL] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:Request1 delegate:self startImmediately:YES]; // flag = true; [SVProgressHUD showWithStatus:@"Wait while we fetch data .."]; if (connection){ NSLog(@"connection success"); completion(true); } else{ // handle error here completion(false); } }