2017-01-31 50 views
0

我triyng從服務器獲取數組。但它不起作用。我的意思是在函數中,一切都與我的數組沒有關係,但在它之外它是空的。怎麼修?客觀c隱形陣列超出功能參數請求

for(int i=1; i<5; i++){ 

    NSString *category = [NSString stringWithFormat:@"%d",i]; 
    NSString *encrypt = @"encrypt=93mrLIMApU1lNM619WzZje4S9EeI4L2L"; 
    NSString *latitude = @"latitude=32.794044"; 
    NSString *longtitude = @"longitude=34.989571"; 
    NSString *params = [NSString stringWithFormat:@"%@&category=%@&%@&%@&area=CENTER", 
        encrypt,category,latitude,longtitude]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:@"http://admin.t-club.co.il/api/get-buissness"]]; 
    NSData *postBody = [params dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:postBody]; 
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) 
    { 
    if(!connectionError) 
    { 
     _myDict =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
     _tmpArray = [_myDict objectForKey:@"result"]; 
     NSLog(@"my array %@",_tmpArray);//here array isn't null 
    } 

    }]; 

    [_myArray addObjectsFromArray:_tmpArray]; 

    } 

NSLog(@"my array %@",_tmpArray);//here is null 

回答

1

它看起來像什麼你的目標的是要在幾個次序異步請求。這可以通過添加一點抽象來完成。

首先,這使得只有一個請求,並提供了在解析的JSON結果的響應字典的方法...

- (void)makeRequestWithParams:(NSString *)params completion:(void (^)(NSDictionary *, NSError *))completion { 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:@"http://admin.t-club.co.il/api/get-buissness"]]; 
    NSData *postBody = [params dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:postBody]; 

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 
     if(!connectionError) { 
      NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
      completion(dictionary, nil); 
     } else { 
      completion(nil, connectionError); 
     } 
    }]; 
} 

請注意,NSURLConnnection方法已取代NSSession,所以這段代碼需要儘快改變。

現在,反覆調用第一個方法的東西。這其中需要的請求參數輸入數組,填充,從詞典結果的可變數組,其完成時調用完成塊...

- (void)makeManyRequestsWithParams:(NSArray *)arrayOfParams fillingArray:(NSMutableArray *)result completion:(void (^)(BOOL))completion { 

    if (arrayOfParams.count == 0) return completion(YES); 

    NSString *nextParams = arrayOfParams[0]; 
    [self makeRequestWithParams:nextParams completion:^(NSDictionary *dictionary, NSError *error) { 
     if (!error && dictionary) { 
      [result addObject:dictionary]; 
      NSArray *remainingParams = [arrayOfParams subarrayWithRange:NSMakeRange(1, arrayOfParams.count-1)]; 
      [self makeManyRequestsWithParams:remainingParams fillingArray:result completion:completion]; 
     } else { 
      completion(NO); 
     } 
    }]; 
} 

最後,你的原始循環的工作,現在只限於組裝參數。一旦這些在陣列中,請致電請求...

- (void)test { 
    NSMutableArray *arrayOfParams = [NSMutableArray array]; 
    for(int i=1; i<5; i++){ 

     NSString *category = [NSString stringWithFormat:@"%d",i]; 
     NSString *encrypt = @"encrypt=93mrLIMApU1lNM619WzZje4S9EeI4L2L"; 
     NSString *latitude = @"latitude=32.794044"; 
     NSString *longtitude = @"longitude=34.989571"; 
     NSString *params = [NSString stringWithFormat:@"%@&category=%@&%@&%@&area=CENTER", 
          encrypt,category,latitude,longtitude]; 
     [arrayOfParams addObject:params]; 
    } 
    NSMutableArray *result = [NSMutableArray array]; 
    [self makeManyRequestsWithParams:arrayOfParams fillingArray:result completion:^(BOOL success) { 
     if (success) { 
      NSLog(@"all done, result is %@", result); 
     } else { 
      NSLog(@"sadness"); 
     } 
    }]; 

    // don't expect results to be ready here. they won't be. 
    // see how they are logged above in the completion block? 
} 
1

NSURLConnection sendAsynchronousRequest是異步的,也就是說它會移動到後臺線程,將繼續執行,而無需等待任務完成。因此,當它達到您的底部NSLog時,請求仍將處理,並且_tmpArray的值將爲null

在繼續之前,您可以使用sendSynchronousRequest來完成請求。

http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/#synchronous

+0

我該如何延遲數組才能滿? –

+0

我已更新我的回答 – tommybananas

+0

我喜歡你原來的答案,但不是編輯。不要用'sendSynchronousRequest'來阻塞主體,特別是在循環中。 OP需要關於如何異步和串行執行這些請求的建議。 – danh