如何减慢ios进程



我有一个可以向服务器发送信息的应用程序。这些信息在白天(当客户端使用应用程序时)会堆积起来,当他需要的时候,他可以点击"更新"按钮在服务器上发送所有信息。

这一直很好,直到他最近流量增加,从更新10个对象增加到超过100个。

显然,更新需要更多的时间,这不是问题所在。

问题是,在某个时候,我得到了

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é."}

对于法国人来说,"请求已过期"是我得到的回复,正如你所注意到的,我已经用***隐藏了url。

现在,我已经在本地尝试过了,它可以很好地进行小更新,但当我在更新时循环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];
    }];
}

我真的不知道你的问题来源,但如果你认为放慢应用程序的速度至少可以帮助你理解你的问题,你可以用这样的方法来解决:

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

它会等待15秒才能继续,所以你可以按照你的建议在20到30个请求后放置这个。

我真的认为你应该考虑分组你的请求或类似的事情,这样你就不会超载你的服务器(如果这真的是你的问题)。

最新更新