我的应用程序是在Objective-C for ios 7中开发的,它(通过插入到NSString中的查询)将自己连接到执行对服务器上数据库的查询的php脚本。我有一些单元格(在tableView中),每个单元格都包含用户信息(ID,名称,描述,个人资料图片)。我每次加载的TableView,我跑3次的查询检索所有元素(和检索资料图片从服务器中的每个用户的方法),那就是:如何提高我的ios应用程序的速度?
NSString *IDUsers = @"Select ID from user";
NSString *Names = @"Select name from user";
NSString *Descriptions = @"Select Description from user";
对于每个字符串(ID用户,名称,说明)我做到这一点:
//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
当我执行查询的数据库throught脚本,它给我返回数组(一个数组每个ID,一个阵列的每个名字,一个阵列的每个描述)。然后,我为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];
这些数组然后填充每个单元格(每个用户的单元格)。 这发生在本地(与MAMP)真的很快。但是,如果连接字符串连接到服务器时发生这种情况,它实际上太慢了。我能怎么做?你有建议吗?我读到我可以用Grand Central Dispatch做,但是怎么做?
请问您可以添加整个代码吗?在此之后查询你在应用程序级别做了什么? –
如果由于网络延迟和网络请求的固有特性而导致速度较慢,Grand Central Dispatch根本没有任何帮助。 – Merlevede
看到编辑....... – Pinturikkio