2016-05-06 13 views
3

私は、必要に応じてサーバからローカルにデータを取得しようとしています。データはさまざまなAPI URLに存在します。私は一回のリフレッシュクリックでそれらのapiをすべて呼び出したいと思います。ループ内で複数の残りのAPIを1つずつ呼び出す方法

-(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 
      } 

} 
+0

、これを試してみてくださいまた、あなたの質問は、どのようにして達成したいのか少しばかり曖昧です。 – rckoenes

+0

あなたがリフレッシュをクリックしたいと希望する情報を提供してください –

+0

私は5分ごとにデータを更新する必要があるプロジェクトに取り組んでいます。私はそれに応じてデータを取得するために多くのAPI URLを持っています。一緒にURLを私は1つずつ応答を取得します。 – vaibhav

答えて

-1

あなたは何を試してみました?

**.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); 
} 

} 
+0

私はあなたのしていることの説明を提供する方が便利かもしれないと思います。その後、vaibhavや他の誰かが同様の問題を抱えている場合は、コードをコピー&ペーストするのではなく、修正する方法を学ぶことができます。 – Ollie

+0

@ Ollie、ここで私はすべてを明確に掲示しました、あなたの疑問は何ですか? – Ravindhiran

+0

あなたがやっていることについてのいくつかのコメントは、将来質問を見つける初心者プログラマーに役立つかもしれないと思います。 – Ollie

関連する問題