Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
由于数据加载,iphone导航速度较慢_Iphone_Xcode_Uinavigationcontroller - Fatal编程技术网

由于数据加载,iphone导航速度较慢

由于数据加载,iphone导航速度较慢,iphone,xcode,uinavigationcontroller,Iphone,Xcode,Uinavigationcontroller,我是objective-c的新手。我的问题是控制器之间的导航速度慢。 当我从一个控制器跳到另一个控制器时,速度非常慢,因为它从服务获取数据,解析数据,然后显示数据。所有这些都需要大约4-5秒。 现在,我已经将所有这些数据提取放在我的DetailController的loadView中。无论如何,我先显示控制器,然后显示数据。因为现在,当我在我的根控制器上选择一行时,它显示活动并停留在根控制器上,然后导航到DetailController。 因此,它首先加载数据,然后显示数据。我是否可以反转此过程

我是objective-c的新手。我的问题是控制器之间的导航速度慢。 当我从一个控制器跳到另一个控制器时,速度非常慢,因为它从服务获取数据,解析数据,然后显示数据。所有这些都需要大约4-5秒。 现在,我已经将所有这些数据提取放在我的DetailController的loadView中。无论如何,我先显示控制器,然后显示数据。因为现在,当我在我的根控制器上选择一行时,它显示活动并停留在根控制器上,然后导航到DetailController。 因此,它首先加载数据,然后显示数据。我是否可以反转此过程,先显示控制器,然后再显示它

这就是我正在做的

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

DataCollection *collection = [DataCollection sharedObject];
Product *tempProduct = [[collection popularProducts] objectAtIndex:row] ;

ProductDetailViewController *tempProductDetail = [[ProductDetailViewController alloc] init];

tempProductDetail.homeViewProduct = tempProduct;
[tempProductDetail release];
}
然后是细节控制器

- (void)loadView {
      [super loadView];
      // here's all the data loading and parsing ... which takes time
}
//下面是下载数据的方法,我使用REST服务获取所有数据

- (void) getProductData:(NSString*)productId{

NSMutableString *specificURL = [[NSMutableString alloc] initWithString:@"GetProductPage?id="];
[specificURL appendFormat:@"%@",productId];
[specificURL appendFormat:@"&dataSources=&accountId=&companyId=&version=1&cultureCode=sv&currencyId=2&format=json"];

NSDictionary *serverCategories = [[NSDictionary alloc] initWithDictionary:[appRequestController proceesRequest:specificURL]];
NSArray *categories = [[[serverCategories objectForKey:@"DataSources"] objectAtIndex:0] objectForKey:@"Items"];
[serverCategories release];

        if ([[productsArray objectAtIndex:j] objectForKey:@"Name"]  != [NSNull null]) {
            [product setProductName:[[productsArray objectAtIndex:j] objectForKey:@"Name"]];
        }
        else {
            [product setProductName:@""];
        }


        if ([[productsArray objectAtIndex:j] objectForKey:@"ThumbnailImage"]  != [NSNull null]) {
            [product setProductImage:[[productsArray objectAtIndex:j] objectForKey:@"ThumbnailImage"]];
        }
        else {
            [product setProductImage:@""];
        }


        if ([[productsArray objectAtIndex:j] objectForKey:@"Price"]  != [NSNull null]) {

            [product setProductPrice:[[productsArray objectAtIndex:j] objectForKey:@"Price"]];      
        }
        else {
            [product setProductPrice:@""];
        }

            [[collection popularProducts] addObject:product];
        [product release];
}

我复制了粘贴的代码,这样你就可以知道我是如何获取数据的。以下是CellForRowatineXpath的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = @"mainMenuIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    [cell setSelectedBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"highlightstrip.png"]]];

}

UILabel *productNameLbl = [[UILabel alloc] initWithFrame:CGRectMake(90, 8, 200,10)];
[productNameLbl setText:[NSString stringWithString:[[[[DataCollection sharedObject] popularProducts]objectAtIndex:indexPath.row] productName]]];
[productNameLbl setFont:[UIFont boldSystemFontOfSize:12.0]];

[cell addSubview:productNameLbl];
[productNameLbl release];

UILabel *productSubLbl = [[UILabel alloc] initWithFrame:CGRectMake(90, 22, 190,40)];
[productSubLbl setText:[NSString stringWithString:[[[[DataCollection sharedObject] popularProducts]objectAtIndex:indexPath.row] productSubHeader]]];
[productSubLbl setTextColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102/255.0 alpha:1.0]];
productSubLbl.lineBreakMode = UILineBreakModeWordWrap;
productSubLbl.numberOfLines = 3;
[productSubLbl setFont:[UIFont fontWithName:@"Arial" size:10]];

[cell addSubview:productSubLbl];
[productSubLbl release];

NSMutableString *url = [[NSMutableString alloc] initWithString:@""];
NSString *baseImageURL = @"http://samplesite.com/";
NSString *imagePath = [NSString stringWithString:[[[[DataCollection sharedObject] popularProducts]objectAtIndex:indexPath.row] productImage]];

[url appendString:baseImageURL];
[url appendString:imagePath];
NSURL *imageURL = [NSURL URLWithString:url];

NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(10, 10, 70, 70)];
[cell addSubview:imageView];
[imageView release];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;

}将url或产品id传递给详细信息视图。在ProductDetail的viewDidLoad中,创建一个后台线程来检索数据。当后台线程完成时,它会将详细信息通知ProductDetail控制器。此时,您将更新ProductDetailView

要在后台线程上获取数据,可以使用


要使视图对用户可用并在一段时间后加载数据,您可以使用NSTimer

只要有一个NSTimer方法,它将在viewDidLoad方法中作为

  • [NSTimer scheduledTimerWithTimeInterval:目标:选择器:用户信息:重复:]
设置一个时间间隔,在此之后调用加载方法


希望这有帮助。

将该行传递到ProductDetailViewController中,并在其viewDidLoad方法中执行提取。您可能还希望显示微调器(UIActivityIndicator),并且您肯定应该读取后台线程。

是否同步加载数据?是的,对于上面的视图。也尝试过异步调用,但没有帮助。不要使用线程,这样会产生太多开销和不必要的设置。使用
NSURLConnection
的异步方法之一我之所以喜欢NSOperation,是因为我可以将所有代码包装成一个整洁的列表子类来处理NSURLConnection。NSURLConnection我必须为我的viewCOntroller创建代理。这样,有了NSW操作子类,我可以在代码中的任何地方使用,而且开销很小。如何先显示视图,然后加载数据???对我来说,线程听起来是个好主意,我的意思是理论上,如果我可以在显示屏幕时在单独的线程中加载数据,这就是我要寻找的。@donito,用下载产品详细信息的方法编辑你的问题,我将编写一个示例NSOperation子类。@black frog,我已经编辑了帖子,并从函数中粘贴了一些数据。我还可以给你发送一个带有详细回复的PM。@kubi如果你正在加载像图像这样的大量数据,你可以在之后调用它们,并显示从服务中获取的其余文本数据,这些数据将比图像更快。在这种情况下,这将非常有用。你能帮我一下吗?有什么问题吗?你为什么要用定时器?为什么不异步下载图像,并在完成后显示它呢?@kubi,异步加载它还有另一个问题。它向我显示屏幕上的图像,当我向下滚动并再次向上滚动时,图像消失并再次获取。是否有任何方法可以存储已获取的图像,以便加载的图像不需要再次加载。我猜您正在
cellforrowatinexpath:
中加载图像?最好的办法可能是将图像下载代码移动到
viewWillLoad
中,并确保您只使用
cellforrowatinexpath
@Kubi中的缓存图像,我将在同一个服务调用中获取所有数据。我不可能再打一个电话只是为了获取图像,这是不可行和不可能的。请告诉我如何分离图像并将其放入viewWillLoad。我已经编辑了我的帖子并粘贴了CellForRowatineXpath的代码。请建议。这是我所做的,但不适用于背景线程。。。