2016-08-19 57 views
0

如果网络不好,我的应用程序在进入前景时卡住图标大约4-5秒。在几乎所有主要的加载页面中,我都从服务器获取图像。我没有任何关于applicationDidEnterBackground()applicationWillEnterForeground()缓慢的应用程序进入前景

在这里,在主视图控制器的代码,我有图像的集合视图:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(homeReuseIdentifier, forIndexPath: indexPath)as! HomeViewCell 

    // Reload the collection view after insert item at indexPath 
    cvHome.reloadItemsAtIndexPaths([indexPath]) 

    let entry = dataCell.infoCell[indexPath.row] 
    cell.backImageView.image = nil 

    let stringURL = "https://.....\(entry.filename)" 

    let image = UIImage(named: entry.filename) 
    cell.backImageView.image = image 

    if cell.backImageView.image == nil { 
     if let image = dataCell.cachedImage(stringURL) { 
      cell.backImageView.image = image 
     } else { 
      request = dataCell.getNetworkImage(stringURL) { image in 
       cell.backImageView.image = image 
      } 
     } 
    } 

    cell.titleLabel.text = entry.title 
    cell.authorLabel.text = entry.author 

    // Fix the collection view cell in size 
    cell.contentView.frame = cell.bounds 
    cell.contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] 

    // To edit the cell for deleting 
    cell.editing = editing 

    return cell 
} 

这是getNetworkImage功能与Alamofire:

func getNetworkImage(urlString: String, completion: (UIImage -> Void)) -> (ImageRequest) { 
    let queue = decoder.queue.underlyingQueue 
    let request = Alamofire.request(.GET, urlString) 
    let imageRequest = ImageRequest(request: request) 
    imageRequest.request.response(
     queue: queue, 
     responseSerializer: Request.imageResponseSerializer(), 
     completionHandler: { response in 
      guard let image = response.result.value else { 
       return 
      } 
      let decodeOperation = self.decodeImage(image) { image in 
       completion(image) 
       self.cacheImage(image, urlString: urlString) 
      } 
      imageRequest.decodeOperation = decodeOperation 
     } 
    ) 
    return imageRequest 
} 
+1

是同步或异步的数据读取? – Harsh

+0

我正在使用Alamofire,所以它是异步 – Giovanni

回答

0

我不知道,如果你的网络代码很糟糕,因为你没有提供任何代码。

我的猜测是,提取数据花费的时间太长,而且会阻止用户界面。所以目前,你的应用程序是同步的。所以任务一次执行一个。你的用户界面被阻止的原因是因为它必须等待网络完成。

您需要做的是在后台执行抓取操作,并始终将您的用户界面保留在主队列中。

0

如果您要调用Synchronous,则必须等到任务完成后才能完成。我们可以在完成当前任务后开始下一个任务。从服务器获取映像时,请确保您使用的是同步还是不同步。您不必等待第二次使用异步服务。您无需等待从服务器获取图像。从服务器获取图像时可以执行其他任务。

对于这个GCD是一个很好的使用。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 
    // Do the back ground process here 
    ......Fetch the image here 
    dispatch_async(dispatch_get_main_queue(), ^{ 
    // Update the UI here 
    .....Do your UI design or updation design part here 
    }); 
}); 

全局队列

异步任务将在这里进行。 它不会等待。 异步函数不会阻止当前正在执行的线程继续执行下一个函数。

主队列

我们必须始终访问主线程UI套件类。

If you don't want to wait a seconds,go with GCD

+0

我实际上使用Alamofire和AlamofireImages应该获取异步。 – Giovanni

+0

我编辑了我的问题,包括代码。 – Giovanni

相关问题