如何使用 dataTaskWithURL:completionHandler 从 PHP 调用检索
How to call retrieve from PHP using dataTaskWithURL:completionHandler
我可以在下面的代码中使用 dataTaskWithURL:completionHandler 吗:
// to call data if there are comments
NSString *checkTitle = self.currentList.qasidaId;
NSLog(@"%@ the title: ", self.currentList.qasidaId);
NSString *url = [NSString stringWithFormat:@"http:/MyWebSite/checkphp.php?title=%@", checkTitle];
NSData *data =[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
self.jasonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
self.listArray = [[NSMutableArray alloc]init];
for (int i = 0; i < self.jasonArray.count; i++) {
NSString *cUserName = [[self.jasonArray objectAtIndex:i]objectForKey:@"userName"];
NSString *cTitle = [[self.jasonArray objectAtIndex:i]objectForKey:@"title"];
NSString *cComments = [[self.jasonArray objectAtIndex:i]objectForKey:@"comments"];
NSString *cTimeC = [[self.jasonArray objectAtIndex:i]objectForKey:@"commentsTime"];
NSString *cDateC = [[self.jasonArray objectAtIndex:i]objectForKey:@"commentsDate"];
NSString *cUserNameArabicC = [[self.jasonArray objectAtIndex:i]objectForKey:@"commentsDate"];
NSString *cTheUserIdC = [[self.jasonArray objectAtIndex:i]objectForKey:@"theUserId"];
[self.listArray addObject:[[ListOfObjects alloc]initWithUserName:cUserName andTitle:cTitle andComments:cComments andtimeC:cTimeC andDateC:cDateC andUserNameArabicC:cUserNameArabicC andTheUserIdC:cTheUserIdC]];
}
[self.tableView reloadData];
}
我需要在上面的代码中使用 dataTaskWithURL:completionHandler,我需要更改什么?
谢谢
您想使用 dataTaskWithURL
并将所有解析逻辑和 table 重新加载逻辑放在 completionHandler
块中:
NSString *checkTitle = self.currentList.qasidaId;
NSLog(@"%@ the title: ", self.currentList.qasidaId);
NSString *urlString = [NSString stringWithFormat:@"http://MyWebSite/checkphp.php?title=%@", checkTitle];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!data) {
NSLog(@"connection error: %@", error);
return;
}
NSError *parseError;
NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&parseError];
if (!json) {
NSLog(@"JSON Parsing error: %@", parseError);
NSLog(@"data = %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
return;
}
NSMutableArray *results = [[NSMutableArray alloc]init];
for (int i = 0; i < json.count; i++) {
NSString *cUserName = json[i][@"userName"];
NSString *cTitle = json[i][@"title"];
NSString *cComments = json[i][@"comments"];
NSString *cTimeC = json[i][@"commentsTime"];
NSString *cDateC = json[i][@"commentsDate"];
NSString *cUserNameArabicC = json[i][@"commentsDate"];
NSString *cTheUserIdC = json[i][@"theUserId"];
[results addObject:[[ListOfObjects alloc]initWithUserName:cUserName andTitle:cTitle andComments:cComments andtimeC:cTimeC andDateC:cDateC andUserNameArabicC:cUserNameArabicC andTheUserIdC:cTheUserIdC]];
}
dispatch_async(dispatch_get_main_queue(), ^{
self.listArray = results;
[self.tableView reloadData];
});
}];
[task resume];
// Just remember, by the time you get here, the above request is
// *not* done. That's why we do the `reloadData` inside the block
// above, and not here.
注意,completionHandler
块 (a) 异步运行; (b) 在后台线程上运行。后一点是为什么我不更新任何 class 属性或从后台线程更新 UI,而是在我们完成解析后手动调用 dispatch_async
回到主线程,其中 (a) 更新 listArray
和 (b) 执行 tableView
的 reloadData
我可以在下面的代码中使用 dataTaskWithURL:completionHandler 吗:
// to call data if there are comments
NSString *checkTitle = self.currentList.qasidaId;
NSLog(@"%@ the title: ", self.currentList.qasidaId);
NSString *url = [NSString stringWithFormat:@"http:/MyWebSite/checkphp.php?title=%@", checkTitle];
NSData *data =[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
self.jasonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
self.listArray = [[NSMutableArray alloc]init];
for (int i = 0; i < self.jasonArray.count; i++) {
NSString *cUserName = [[self.jasonArray objectAtIndex:i]objectForKey:@"userName"];
NSString *cTitle = [[self.jasonArray objectAtIndex:i]objectForKey:@"title"];
NSString *cComments = [[self.jasonArray objectAtIndex:i]objectForKey:@"comments"];
NSString *cTimeC = [[self.jasonArray objectAtIndex:i]objectForKey:@"commentsTime"];
NSString *cDateC = [[self.jasonArray objectAtIndex:i]objectForKey:@"commentsDate"];
NSString *cUserNameArabicC = [[self.jasonArray objectAtIndex:i]objectForKey:@"commentsDate"];
NSString *cTheUserIdC = [[self.jasonArray objectAtIndex:i]objectForKey:@"theUserId"];
[self.listArray addObject:[[ListOfObjects alloc]initWithUserName:cUserName andTitle:cTitle andComments:cComments andtimeC:cTimeC andDateC:cDateC andUserNameArabicC:cUserNameArabicC andTheUserIdC:cTheUserIdC]];
}
[self.tableView reloadData];
}
我需要在上面的代码中使用 dataTaskWithURL:completionHandler,我需要更改什么?
谢谢
您想使用 dataTaskWithURL
并将所有解析逻辑和 table 重新加载逻辑放在 completionHandler
块中:
NSString *checkTitle = self.currentList.qasidaId;
NSLog(@"%@ the title: ", self.currentList.qasidaId);
NSString *urlString = [NSString stringWithFormat:@"http://MyWebSite/checkphp.php?title=%@", checkTitle];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!data) {
NSLog(@"connection error: %@", error);
return;
}
NSError *parseError;
NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&parseError];
if (!json) {
NSLog(@"JSON Parsing error: %@", parseError);
NSLog(@"data = %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
return;
}
NSMutableArray *results = [[NSMutableArray alloc]init];
for (int i = 0; i < json.count; i++) {
NSString *cUserName = json[i][@"userName"];
NSString *cTitle = json[i][@"title"];
NSString *cComments = json[i][@"comments"];
NSString *cTimeC = json[i][@"commentsTime"];
NSString *cDateC = json[i][@"commentsDate"];
NSString *cUserNameArabicC = json[i][@"commentsDate"];
NSString *cTheUserIdC = json[i][@"theUserId"];
[results addObject:[[ListOfObjects alloc]initWithUserName:cUserName andTitle:cTitle andComments:cComments andtimeC:cTimeC andDateC:cDateC andUserNameArabicC:cUserNameArabicC andTheUserIdC:cTheUserIdC]];
}
dispatch_async(dispatch_get_main_queue(), ^{
self.listArray = results;
[self.tableView reloadData];
});
}];
[task resume];
// Just remember, by the time you get here, the above request is
// *not* done. That's why we do the `reloadData` inside the block
// above, and not here.
注意,completionHandler
块 (a) 异步运行; (b) 在后台线程上运行。后一点是为什么我不更新任何 class 属性或从后台线程更新 UI,而是在我们完成解析后手动调用 dispatch_async
回到主线程,其中 (a) 更新 listArray
和 (b) 执行 tableView
reloadData