简体   繁体   English

如何减慢iOS进程

[英]How to slow down ios process

I have an app that can send information to a server. 我有一个可以将信息发送到服务器的应用程序。 This information is stacked up during the day (while the client uses the app), and when he so desires, he can hit the "update" button to send everything on the server. 白天(当客户端使用该应用程序时),此信息会堆积起来,并且在他愿意时,他可以单击“更新”按钮以将所有内容发送到服务器上。

This always worked fine until he recently had a flow increase and went from updating 10 objects to more than 100. 在他最近增加流量并将其从更新10个对象增加到100个以上之前,这一直很好。

Obviously, the update takes more time, taht's not the issue. 显然,更新需要更多时间,这不是问题。

The issue is, at some point, i'm getting 问题是,在某个时候,我越来越

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

For the frenchophobes, " The request has expired " is what i get back, and i've hidden the url with ****, as you noticed. 对于法国人,我收到了“请求已过期”的信息,并且正如您注意到的那样,我用****隐藏了该网址。

Now, i've tried locally, it works fine with a small update, but when i loop 150 times on my update (i send 150 times the same thing), at some point i just get the above error X times. 现在,我在本地尝试过,在进行较小的更新后仍能正常工作,但是当我在更新上循环150次(我发送150次相同的东西)时,有时我会得到上述错误X倍。 This error does not specificall occur with all the last items, it can be 20 in the middle, or 30, etc. 最后的所有项目都不会发生此错误,中间可能是20,也可能是30,依此类推。

Is there a way i can change that? 有办法改变吗?

Here is a piece of code that must be related to the issue. 这是与该问题相关的一段代码。

// 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];
        }}

Now what would probably work is put the last line in a "thing" that would slow down the process every 20 or so occurences, so the server or the app doesn't go crazy. 现在可能会起作用的方法是将最后一行放在“事物”中,该事物每20次左右发生就会减慢该过程的速度,因此服务器或应用程序不会发疯。

Is this possible? 这可能吗? if so, how? 如果是这样,怎么办?

Note : I am a junior dev trying to get into a senior's code, and that guy is not available, so i'm open to all the help i can have. 注意:我是一名初级开发人员,试图获取高级代码,而该人员不可用,因此我愿意提供所有帮助。

Edit : also, do you think my error comes from a server-sided issue or is definitly an app-sided issue? 编辑:另外,您是否认为我的错误是来自服务器端的问题还是绝对是应用程序端的问题?

Edit : Complete HTTP request. 编辑:完成HTTP请求。 So for every person that is saved into the app, when the user decides to update, it does that for every Person in the array of persons. 因此,对于保存到应用程序中的每个人,当用户决定更新时,它都会对一系列人中的每个人进行更新。

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

I don't really know the source of your problem, but if you think slowing the app will at least help you understand your problem you could do it with something like this: 我真的不知道问题的根源,但是如果您认为降低应用程序速度至少可以帮助您了解问题,则可以使用以下方法来解决:

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

It will wait for 15 seconds before continue, so you can put this one after 20~30 requests as you suggested. 它将等待15秒钟,然后才能继续,因此您可以按照建议在20到30个请求后放入此请求。

I really believe you should consider grouping your requests or something like that so you won't overload your server (if that is really your problem). 我真的相信您应该考虑对您的请求或类似的东西进行分组,以免服务器超载(如果这确实是您的问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM