2014-09-02 47 views
0

我有一個可以發送信息到服務器的應用程序。這些信息會在白天堆疊起來(當客戶使用應用程序時),當他想要時,他可以點擊「更新」按鈕發送服務器上的所有內容。如何減慢ios進程

這一直工作得很好,直到他最近有一個流量的增加和更新10個物體超過100

顯然去的時候,更新需要花費更多的時間,taht這不是問題。

的問題是,在某些時候,我得到

Error: Error Domain=NSURLErrorDomain Code=-1001 "La requête a expiré." 
UserInfo=0x189874b0 {NSErrorFailingURLStringKey=http://www.*********.be/upload, 
NSErrorFailingURLKey=http://www.************.be/upload, 
NSLocalizedDescription=La requête a expiré., 
NSUnderlyingError=0x189abd70 "La requête a expiré."} 

對於frenchophobes「的請求已過期」是什麼我回來了,我已經隱藏的網址上*** *,正如你注意到的那樣。

現在,我已經在本地嘗試過了,它可以很好地進行小更新,但是當我在更新(我發送150次相同的東西)上循環150次時,在某些時候,我只是得到了上述錯誤X次。這個錯誤並不是所有最後的項目都會發生的,它可以是中間的20或30等。

有沒有辦法改變這種情況?

這是一段必須與問題相關的代碼。

// Set the max number of concurrent operations (threads) 
    //[operationQueue setMaxConcurrentOperationCount:3]; // Todo: try increasing max thread count 
    [operationQueue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount]; //dynamic thread count 
    self.queueCount = persons.count; 
    self.currentQueue = 1; 

    for (Person *person in persons) { 
     for (int i = 0 ; i<130 ; i++){ //this is where i try to break the app 
     [self createSendPersonOperation:person]; 
     }} 

現在什麼可能會工作放在一個「東西」,將過程減慢每20個左右出現次數的最後一行,因此服務器或應用程序不發瘋。

這可能嗎?如果是這樣,怎麼樣?

注意:我是一名初級開發人員,想要進入一位高級代碼,並且該人員不可用,所以我願意接受我所能提供的所有幫助。

編輯:另外,你認爲我的錯誤來自服務器端的問題,還是明確的應用程序的問題?

編輯:完整的HTTP請求。 因此,對於每個保存到應用程序中的用戶,當用戶決定更新時,都會爲該人員陣列中的每個人員執行此操作。

- (void)createSendPersonOperation:(Person *)person 
{ 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", nil]; 

    NSDictionary *params = @{ 
     @"email": person.email, 
     @"gender": person.gender, 
     @"language": person.language, 
     @"hasFacebook": person.hasFacebook, 
     @"sendPostalCard": person.sendPostalCard 
    }; 

    NSLog(@"params: %@", params); 

    [manager POST:kURLUpdate parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
     // Add picture to the form 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 

     NSString *pictureFilePath = [documentsDirectory stringByAppendingPathComponent:person.picture]; 
     NSURL *pictureURL = [NSURL fileURLWithPath:pictureFilePath]; 

     [formData appendPartWithFileURL:pictureURL name:@"picture" error:nil]; 
    } success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Success: %@", responseObject); 

     if ([responseObject isKindOfClass:[NSDictionary class]]) { 
      if ([responseObject objectForKey:@"error"]) { 
       NSLog(@"Error 1"); 
       NSDictionary *error = [responseObject objectForKey:@"error"]; 
       NSLog(@"Error message: %@", [error objectForKey:@"message"]); 
      } else { 
       // Set Person's sended attribute 
       person.sended = @YES; 

       [Person saveObject:[[PersistentStack sharedInstance] managedObjectContext] error:nil]; 
      } 
     } else { 
      NSLog(@"Error 2"); 
     } 

     [self decreaseQueueCount]; 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
     NSLog(@"Parameter that failed : %@", [params objectForKey:@"email"]); 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Erreur" 
                  message:[error localizedDescription] 
                  delegate:nil 
                cancelButtonTitle:@"Fermer" 
                otherButtonTitles:nil]; 
     [alertView show]; 

     self.updateHud.mode = MBProgressHUDModeText; 
     self.updateHud.labelText = AMLocalizedString(@"update.failure.message", @""); 

     [self.updateHud hide:YES afterDelay:3]; 
    }]; 
} 
+0

是這些單獨的http請求嗎?我最初的猜測是,你可能會溢出你的服務器,它無法處理它們,但你真的不想做這樣的事情。 – timpone 2014-09-02 14:07:58

+0

我更新了我的問題;它們應該是單獨的HTTP請求,因爲該方法執行了X次。 – 2014-09-02 14:15:29

回答

0

我真的不知道你的問題的根源,但如果你認爲減慢應用程序將至少幫助你理解你的問題,你可以使用像這樣做:

NSDate *loopUntil = [NSDate dateWithTimeIntervalSinceNow:15]; 
while ([loopUntil timeIntervalSinceNow] > 0) { 
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode 
          beforeDate:loopUntil]; 
} 

它會等待15秒才能繼續,因此您可以按照您的建議在20〜30個請求之後放置一個。

我真的相信你應該考慮分組你的請求或類似的東西,所以你不會超載你的服務器(如果這真的是你的問題)。