我正在构建一个类似于iPhone上的Apples Photos应用程序的应用程序。使用Master-Detail界面,用户可以选择一个相册并转到显示一组照片的UICollectionViewController。对于主视图和详细视图控制器,我使用NSFetchedResultsControllers来访问数据库中的对象。现在不幸的是,当选择一个由NSFetchedResultsController获取项目的细节引起的相册时,会有一个非常明显的延迟。NSFetchedResultsController导致延迟的延迟
由于苹果照片应用程序立即响应,并在点击时准备好所有照片,我想知道他们是如何做到的。
对此有最佳做法吗?
代码示例
//DetailViewController.swift
// lazy frc
var fetchedResultsController: NSFetchedResultsController {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
let fetchRequest = NSFetchRequest()
// Edit the entity name as appropriate.
let entity = NSEntityDescription.entityForName("Photo", inManagedObjectContext: self.context)
fetchRequest.entity = entity
// Set the batch size to a suitable number.
fetchRequest.fetchBatchSize = 20
// Edit the sort key as appropriate.
let sortDescriptor = NSSortDescriptor(key: "photoID", ascending: false)
let sortDescriptors = [sortDescriptor]
fetchRequest.sortDescriptors = [sortDescriptor]
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.context, sectionNameKeyPath: nil, cacheName: "ernesto")
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
var error: NSError? = nil
if !_fetchedResultsController!.performFetch(&error) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
//println("Unresolved error \(error), \(error.userInfo)")
abort()
}
return _fetchedResultsController!
}
var _fetchedResultsController: NSFetchedResultsController? = nil
// MARK: - UICollectionViewDataSource
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! PhotoCell
let item = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
cell.backgroundColor = UIColor.yellowColor()
//the following line is commented out intentionally to make sure the delay is not caused by setting the image
//cell.imageView.image = UIImage(data: item.valueForKey("image") as! NSData)
return cell
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let sectionInfo = self.fetchedResultsController.sections![section] as! NSFetchedResultsSectionInfo
return sectionInfo.numberOfObjects
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
最好的做法是展示一些代码。 iOS通常足够快,你不会感到延迟,除非你做一些腥意...... – luk2302
我编辑我的问题,包括示例代码。 – matteok
您是否将图像存储在核心数据中? – Mika