简体   繁体   English

如何提高ios应用程序的速度?

[英]How can I improve the speed of my ios app?

My app is developed in Objective-C for ios 7, and it (throught queries inserted in NSString) connects itself to the php script that execute queries on the database on the server. 我的应用程序是在Objective-C for ios 7中开发的,它(通过插入NSString中的查询)将自身连接到php脚本,该脚本在服务器上的数据库上执行查询。 I've some cells (in a tableView) that each contain the information of a user (ID, name, description, profile picture). 我有一些单元格(在tableView中),每个单元格都包含用户的信息(ID,名称,描述,个人资料图片)。 Every time I load the TableView, I run 3 queries that retrieve all the elements (and a method that retrieves profile pictures for each user from the server), that is: 每次加载TableView时,我都会运行3个查询来检索所有元素(以及从服务器检索每个用户的个人资料图片的方法),即:

NSString *IDUsers = @"Select ID from user";
NSString *Names = @"Select name from user";
NSString *Descriptions = @"Select Description from user";

For each string (IDUser,Names,Descriptions) I do it: 对于每个字符串(IDUser,Names,Descriptions),我这样做:

//query can be IDUser or Names or Descriptions
NSMutableString *strURL = [NSMutableString stringWithFormat:@"http://localhost/myApp/selectquery.php?query=%@",query];

[strURL setString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

NSError* error;

NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:dataURL
                      options:kNilOptions
                      error:&error];

NSMutableArray *results = [[NSMutableArray alloc] init];

int numRow = 0;

for (NSArray *arrow in json) {
    [results addObjectsFromArray:arrow];
    numRow++;
}

//results is the array of results of the query

When I execute the queries on db throught the script, it give me Back arrays (one array for each ID, one array for each name, one array for each description). 当我通过脚本在db上执行查询时,它会给我返回数组(每个ID一个数组,每个名称一个数组,每个描述一个数组)。 Then I execute a for cycle for each ID in array of ID and I retrieve the profile picture associated to each ID (from server). 然后我为ID数组中的每个ID执行for循环,然后检索与每个ID(来自服务器)关联的配置文件图片。

NSMutableString *strURL = [NSMutableString stringWithFormat:@"http://localhost/myApp/getProfilePicture.php?IDUser=%@",arrID[i]];

    [strURL setString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

    UIImage *profileSnap = [UIImage imageWithData:dataURL];

These arrays then populate each cell (a cell for each user). 然后这些数组填充每个单元格(每个用户的单元格)。 This, occurred locally (with MAMP) is really fast. 这在本地(与MAMP)发生的确非常快。 But if this happens when the connection string is connected to the server, it's really too slow. 但是,如果连接字符串连接到服务器时发生这种情况,那就太慢了。 How can I do? 我能怎么做? Have you got suggestions? 你有建议吗? I read that I could do with Grand Central Dispatch, but how? 我读到我可以用Grand Central Dispatch做,但是怎么做?

If the slow speed is because of the network latency and the inherent nature of web requests, Grand Central Dispatch doesn't help at all. 如果速度慢是由于网络延迟和Web请求的固有特性,Grand Central Dispatch根本没有帮助。

You can follow the correct example suggested by emotality in his answer, to perform some lazy loading, specially for images. 您可以按照他的答案中的情感性建议的正确示例来执行一些延迟加载,特别是对于图像。 GCD allows you to do tasks in the background to improve the user experience, but it won't speed up the web requests. GCD允许您在后台执行任务以改善用户体验,但它不会加速Web请求。

If your app is slow because it is processing a lot of data there are other strategies that you can follow. 如果您的应用程序因处理大量数据而变慢,则可以遵循其他策略。 For example if your app processes 100,000 records, you could modify your web service to be queried a subset of the data, so you can present immediately to the user the first n records, while you still download more in the background. 例如,如果您的应用程序处理100,000条记录,您可以修改您的Web服务以查询数据的子集,这样您就可以立即向用户显示前n条记录,同时您仍然可以在后台下载更多记录。

I hope this helps you in some way, but you need to provide more details about why is your application slow, and for each reason there's a different strategy. 我希望这会以某种方式帮助您,但您需要提供有关您的应用程序为何缓慢的更多详细信息,并且由于每个原因,都有不同的策略。

EDIT: 编辑:
AS I said, you're web service needs to support it, if it doesn't you need to change it and design some way of doing it. 正如我所说,你是Web服务需要支持它,如果不是你需要改变它并设计一些方法。 On way would be passing other parameters in the querystring of your request (these parameters would be rangeStart and rangeEnd , for example). 在途中将传递请求的查询字符串中的其他参数 (例如,这些参数将是rangeStartrangeEnd )。 Your php script needs to recognize these parameters and query your database for records corresponding to that specific range. 您的php脚本需要识别这些参数并在数据库中查询与该特定范围相对应的记录。

Obviously this complicates your design, because you need to keep track of which ranges you have already queried, but I don't think it's thaaaat difficult either. 显然这会使你的设计变得复杂,因为你需要跟踪你已经查询过的范围,但我认为这也不困难。

This is called lazy loading, it runs (downloading) in the background while your tableView is loading all other data from delegate etc :) 这称为延迟加载,它在后台运行(下载),而tableView正在从委托加载所有其他数据等:)

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{

// get image from server here

        dispatch_sync(dispatch_get_main_queue(), ^{

            // set images after they have been downloaded/saved.
            // ALWAYS USE MAIN_QUEUE WHEN WORKING WITH VISIBLE ELEMENTS LIKE THESE

            [cell.imageView setImage:(UIImage *)];
            [cell setNeedsLayout];
        });

});

[self.tableView reloadData];

Remember to reload tableview data when finished. 记得在完成后重新加载tableview数据。 Hope this helps! 希望这可以帮助!

For the fastest webservice calls, use MKNetworkKit 对于最快的Web服务调用,请使用MKNetworkKit

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

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