gpt4 book ai didi

ios - 为什么在检索 json 数据和显示 viewcontroller 之间有很长的延迟

转载 作者:行者123 更新时间:2023-12-01 16:40:03 25 4
gpt4 key购买 nike

我在 JSON 数据检索和 UITableViewController 启动之间存在长时间延迟的问题。

下面的方法使用从 UITableViewControllers 初始化程序调用的硬编码查询,并在 2 秒内检索和显示数据。

- (void)productsQuery
{
NSString *requestString = @"http://192.168.2.10/testQueries.php?Product_Description=tea";
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

NSURLSessionDataTask *dataTask =
[self.session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {

NSArray *returnedItems =
[NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&error];

for (int i = 0; i < [returnedItems count]; i++) {

NSDictionary *item = [returnedItems objectAtIndex:i];

NSNumber *nBay = [item objectForKey:@"Bay_Number"];
NSNumber *nShelf = [item objectForKey:@"Shelf_Number"];
NSNumber *coordX = [item objectForKey:@"CoordinateX"];
NSNumber *coordY = [item objectForKey:@"CoordinateY"];

TNWProduct *product =[[TNWProduct alloc]
initWithProductDescription:[item objectForKey:@"Product_Description"]
aisleNumber:[item objectForKey:@"Aisle_Number"]
bay:[nBay intValue]
shelf:[nShelf intValue]
nonAisleLocation:[item objectForKey:@"Location_Description"]
coordinateX:[coordX intValue]
coordinateY:[coordY intValue]];

[self.productList addObject:product];
}

NSLog(@"%@", self.productList);

dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}];

[dataTask resume];
}

该方法随后被调整并移动到 UIView Controller ,以便用户可以输入查询。

JSON 数据仍会在 2 秒内被检索并添加到 NSMutableArray _productList 中,正如它在 NSLog 调用的控制台中显示的那样,但在启动 ProductListViewController 之前,它似乎在 5-15 秒内什么都不做。
@interface TNWSearchViewController () <UITextFieldDelegate>

@property (weak, nonatomic) NSString *userQuery;
@property (weak, nonatomic) IBOutlet UIToolbar *toolbar;
@property (weak, nonatomic) IBOutlet UITextView *informationMessages;
@property (nonatomic) NSURLSession *session;
@property (nonatomic, strong) NSMutableArray *productList;
@property (nonatomic, strong) NSArray *returnedItems;

@end

@implementation TNWSearchViewController
.
.
.
.

- (void)productQuery:(NSString *)query
{
if ([_productList count] > 0 ) {
[_productList removeAllObjects];
}

NSMutableString *requestString = [@"http://192.168.2.10/testQueries.php?Product_Description=" mutableCopy];

[requestString appendString:query];

NSString *escapedRequestString = [requestString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSLog(@"%@", escapedRequestString);

NSURL *url = [NSURL URLWithString:escapedRequestString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

NSURLSessionDataTask *dataTask =
[self.session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {

_returnedItems =
[NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&error];
//NSLog(@"Returned items = %@", _returnedItems);
for (int i = 0; i < [_returnedItems count]; i++) {

NSDictionary *item = [_returnedItems objectAtIndex:i];
//NSLog(@"Item = %@", item);
NSNumber *nBay = [item objectForKey:@"Bay_Number"];
NSNumber *nShelf = [item objectForKey:@"Shelf_Number"];
NSNumber *coordX = [item objectForKey:@"CoordinateX"];
NSNumber *coordY = [item objectForKey:@"CoordinateY"];

TNWProduct *product =[[TNWProduct alloc]
initWithProductDescription:[item objectForKey:@"Product_Description"]
aisleNumber:[item objectForKey:@"Aisle_Number"]
bay:[nBay intValue]
shelf:[nShelf intValue]
nonAisleLocation:[item objectForKey:@"Location_Description"]
coordinateX:[coordX intValue]
coordinateY:[coordY intValue]];
NSLog(@"%@", product);
[_productList addObject:product];

}

NSLog(@"Product list = %@", self.productList);

if ( [_productList count] > 0 ) {

TNWProductListViewController *plvc =
[[TNWProductListViewController alloc] initWithStyle:UITableViewStylePlain];

plvc.productList = [self.productList mutableCopy];

[self.navigationController pushViewController:plvc animated:YES];

} else {
_informationMessages.text = @"No matches found";
}
}];

[dataTask resume];
}

将代码块从 for 循环和 if/else 语句中移出 [dataTask resume]导致应用程序按预期加载 UITableView,但无法再访问 _returnedItems 中的数据。

协助表示赞赏。
for (int i = 0; i < [_returnedItems count]; i++) {
.
.
.
} else {
_informationMessages.text = @"No matches found";
}

最佳答案

将 ViewController 的创建和调用移至 dispatch_async block ,如下所示解决了该问题。

谢谢福尼克斯。

dispatch_async(dispatch_get_main_queue(), ^{
if ( [[[TNWProductList productsStore] allProducts] count] > 0 ) {

TNWProductListViewController *plvc =
[[TNWProductListViewController alloc] initWithStyle:UITableViewStylePlain];


[self.navigationController pushViewController:plvc animated:YES];

} else {
_informationMessages.text = @"No matches found";
}

关于ios - 为什么在检索 json 数据和显示 viewcontroller 之间有很长的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25173728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com