2011-06-21 6 views
0

アプリケーションを起動するたびに20秒待たずにAPIからデータをロードする必要があります。非同期要求から正しい順序でデータを取得

だから私は使用:

NSURL *myUrlCourses = [[NSURL alloc] initWithString:url]; 
NSMutableURLRequest *request = [NSURLRequest requestWithURL: myUrlCourses]; 
NSURLConnection *connexion = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

バックグラウンドでデータを取得することを許可し、whileループの私の10の最初の要求のために。私はこの要求からデータを取得するとき しかしは、:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{} 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{} 

結果が一致していません。 だから、私は、スレッドごと、またはそのようなものを使って各リクエストの正しいデータを取得しなければならないと思いますが、どうしたらいいのか分かりません!

この問題の解決にお役立てください。

ありがとう

答えて

1

CFMutableDictionaryRefを使用して、接続ごとに別の変更可能な辞書を保存します。この内部辞書は、必要なだけ多くのデータを保持できます。このような

@interface Foo { 
    CFMutableDictionaryRef connections; 
} 


@implementation Foo 

- (id)init { 
    self = [super init]; 
    if (self) { 
     connections = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 
    } 
    return self; 
} 

- (BOOL)addURLRequest:(NSURLRequest *)request successSelector:(SEL)successSelector errorSelector:(SEL)errorSelector { 
    NSMutableDictionary *connectionInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
              [NSMutableData data], @"receivedData", 
              [NSValue valueWithPointer:successSelector], @"successSelector", 
              [NSValue valueWithPointer:errorSelector], @"errorSelector", 
              request, @"request", 
              nil]; 
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease]; 
    CFDictionaryAddValue(connections, connection, connectionInfo); 
    [connection start]; 
    return YES; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    NSMutableDictionary *connectionInfo = (NSMutableDictionary *)CFDictionaryGetValue(connections, connection); 
    [[connectionInfo objectForKey:@"receivedData"] appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSMutableDictionary *connectionInfo = (NSMutableDictionary *)CFDictionaryGetValue(connections, connection); 
    NSData *data = [connectionInfo objectForKey:@"receivedData"]; 
    LogInfo(@"Finished Connection %@", connection); 
    SEL selector = [[connectionInfo objectForKey:@"successSelector"] pointerValue]; 
    if ([self respondsToSelector:selector]) { 
     [self performSelector:selector withObject:data]; 
    } 
    CFDictionaryRemoveValue(connections, connection); 
} 
+0

実装し、それは完全にコード他に何がたくさん出て、私のニーズに合わせて非常に簡単に...! ありがとうございます:) – Guillaume

2

すべての接続を説明変数で変数に格納し、ポインタ値を比較します。私は、要求応答と周りの移動、追加データを指定し[request setUserInfo:(NSDictionary *)]を使用することができますので、私はこの種のもののためにASIHTTPRequestを使用

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    if(connection == _theFacebookConnection) 
    { 
     //Handle Facebook code 
    } 
    else if(connection == _theTwitterConnection) 
    { 
     //Handle Twitter code 
    } 
} 
1

私はそれぞれの応答を受け取ったとき、私はUserInfo辞書を要求し、それに応じてデータを処理することができます。

これは素晴らしいことですが、必要に応じてUserInfoディクショナリに大量のデータを入れることができます。

関連する問題