我的應用程序是在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