Iphone 在UITableView中导航速度慢,JSON问题

Iphone 在UITableView中导航速度慢,JSON问题,iphone,ios,json,uitableview,nsjsonserialization,Iphone,Ios,Json,Uitableview,Nsjsonserialization,我正在工作的表视图中加载4个城市字符串,但当我选择一个单元格并导航到另一个表时,它的导航速度太慢。我在另一个表中使用了与下面相同的代码和不同的链接。你能告诉我为什么要花很长时间(~4-6秒)才能到达另一个视图吗 - (void)viewDidLoad { [super viewDidLoad]; NSURL * url = [NSURL URLWithString:@"http://kalkatawi.com/jsonTest.php"]; NSData * data = [NSData

我正在工作的表视图中加载4个城市字符串,但当我选择一个单元格并导航到另一个表时,它的导航速度太慢。我在另一个表中使用了与下面相同的代码和不同的链接。你能告诉我为什么要花很长时间(~4-6秒)才能到达另一个视图吗

- (void)viewDidLoad
{
[super viewDidLoad];

NSURL * url  = [NSURL URLWithString:@"http://kalkatawi.com/jsonTest.php"];

NSData  * data = [NSData dataWithContentsOfURL:url];

NSError *e = nil;

jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];

jsonArray1 = [[NSMutableArray alloc] init];

for(int i=0;i<[jsonArray count];i++)
{                
    NSString  * city = [[jsonArray objectAtIndex:i] objectForKey:@"city"];

    [jsonArray1 addObject:city];
}
-


可能是因为您正在下载同步,并且您正在阻止主线程,可能这就是您的应用程序冻结4-6秒的原因,请尝试下载您的json异步

- (void)viewDidLoad
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSData *response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://kalkatawi.com/jsonTest.php"]];
    NSError *parseError = nil;
    jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError];
    jsonArray1 = [[NSMutableArray alloc] init]
        for(int i=0;i<[jsonArray count];i++)
        {                
          NSString  * city = [[jsonArray objectAtIndex:i] objectForKey:@"city"];

          [jsonArray1 addObject:city];
        }
    }
    dispatch_sync(dispatch_get_main_queue(), ^{
            [self.myTableView reloadData];
        });
});
}
-(void)viewDidLoad
{
调度异步(调度获取全局队列(调度队列优先级默认为0)^{
NSData*响应=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@]http://kalkatawi.com/jsonTest.php"]];
NSError*parseError=nil;
jsonArray=[NSJSONSerialization JSONObjectWithData:响应选项:NSJSONReadingAllowFragments错误:&parseError];
jsonArray1=[[NSMutableArray alloc]init]

对于(int i=0;i在另一个屏幕上导航需要更多的时间,因为您正在从服务器同步加载数据。在iOS中,所有UI都在主线程上完成,并且通过在主线程上进行数据调用,您正在阻止它。我知道处理此问题的最佳方法是使用GCD(Grand Central Dispatch)。这是iOS中的一个API,可以轻松地为您生成线程。您只需告知是否要在后台线程上调用以从服务器加载数据。执行此操作时,视图应立即导航。数据到达时,您可以使用活动指示器

dispatch_async(dataQueue, ^{

        // Load all your data here

        dispatch_async(dispatch_get_main_queue(), ^{
            // Update the UI

        });

    }); 

听起来你正在做的是在主线程上进行同步网络调用。查找异步网络调用的方法。有很多例子。NSData的
initWithContentsOfURL:
不应用于访问远程资源。文档中对这一事实相当沉默,但苹果官方开发人员forum苹果公司的工程师们有很多声明称,
initWithContentsOfURL:
方法系列只能用于文件访问。使用
NSURLConnection
通过网络访问远程资源。@CouchDeveloper@CarlosVela现在它工作得更好了,比以前更快了。我已经提到过,首先是它显示
UITableView
,然后加载数据。是否仍有改进此问题的方法?@LuaiKalkatawi尝试使用活动指示器指示用户正在从web服务下载数据。
- (void)viewDidLoad
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSData *response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://kalkatawi.com/jsonTest.php"]];
    NSError *parseError = nil;
    jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError];
    jsonArray1 = [[NSMutableArray alloc] init]
        for(int i=0;i<[jsonArray count];i++)
        {                
          NSString  * city = [[jsonArray objectAtIndex:i] objectForKey:@"city"];

          [jsonArray1 addObject:city];
        }
    }
    dispatch_sync(dispatch_get_main_queue(), ^{
            [self.myTableView reloadData];
        });
});
}
dispatch_async(dataQueue, ^{

        // Load all your data here

        dispatch_async(dispatch_get_main_queue(), ^{
            // Update the UI

        });

    });