Automates prefetching of content in UITableView and UICollectionView

Overview

Automates preheating (prefetching) of content in UITableView and UICollectionView.

Deprecated on iOS 10. This library is similar to UITableViewDataSourcePrefetching and UICollectionViewDataSourcePrefetching added in iOS 10 which I would recommend to use instead.

One way to use Preheat is to improve user experience in applications that display collections of images. Preheat allows you to detect which cells are soon going to appear on the display, and prefetch images for those cells. You can use Preheat with any image loading library, including Nuke which it was designed for.

The idea of automating preheating was inspired by Apple’s Photos framework example app.

Getting Started

Usage

Here is an example of how you might implement preheating in your application using Preheat and Nuke:

import Preheat
import Nuke

class PreheatDemoViewController: UICollectionViewController {
    let preheater = Nuke.Preheater()
    var controller: Preheat.Controller<UICollectionView>?

    override func viewDidLoad() {
        super.viewDidLoad()

        controller = Preheat.Controller(view: collectionView!)
        controller?.handler = { [weak self] addedIndexPaths, removedIndexPaths in
            self?.preheat(added: addedIndexPaths, removed: removedIndexPaths)
        }
    }

    func preheat(added: [IndexPath], removed: [IndexPath]) {
        func requests(for indexPaths: [IndexPath]) -> [Request] {
            return indexPaths.map {
                var request = Request(url: photos[$0.row])
                request.priority = .low
                return request
            }
        }
        preheater.startPreheating(with: requests(for: added))
        preheater.stopPreheating(with: requests(for: removed))
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        controller?.enabled = true
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)

        // When you disable preheat controller it removes all preheating 
        // index paths and calls its handler
        controller?.enabled = false
    }
}

Requirements

  • iOS 8.0 / tvOS 9.0
  • Xcode 9
  • Swift 4

Installation

CocoaPods

To install Preheat add a dependency to your Podfile:

# source 'https://github.com/CocoaPods/Specs.git'
# use_frameworks!
# platform :ios, "8.0"

pod "Preheat"

Carthage

To install Preheat add a dependency to your Cartfile:

github "kean/Preheat"

Import

Import installed modules in your source files

import Preheat

License

Preheat is available under the MIT license. See the LICENSE file for more info.

Comments
  • Could not cast value of type 'FlexiCollectionViewLayout.FlexiCollectionViewLayout'  to 'UICollectionViewFlowLayout'

    Could not cast value of type 'FlexiCollectionViewLayout.FlexiCollectionViewLayout' to 'UICollectionViewFlowLayout'

    Привет. Я не силен в swift, только начинаю его изучать, прошу помочь. Я использую кастомный layout (FlexiCollectionViewLayout)

    Как следствие, приложение падает при попытке cast типов

    Could not cast value of type 'FlexiCollectionViewLayout.FlexiCollectionViewLayout' (0x103ffe070) to 'UICollectionViewFlowLayout' (0x1055a2a48).

    Падает вот в этом месте extension UICollectionView: Preheated { public var orientation: ScrollOrientation { switch (collectionViewLayout as! UICollectionViewFlowLayout).scrollDirection { case .vertical: return .vertical case .horizontal: return .horizontal } }

    Падает потому, что я использую FlexiCollectionViewLayout, тогда как Preheared желает получить layout как UICollectionViewFlowLayout

    Смысл проблемы понятен, но как исправить, я не понимаю, очень прошу помочь.

    Вот кусок кода загрузки контроллера...

    override func viewDidLoad() { super.viewDidLoad()

        preheater = Preheater()
        preheatController = Preheat.Controller(view: self.collectionView!)
        preheatController.handler = { [weak self] addedIndexPaths, removedIndexPaths in
            self?.preheat(added: addedIndexPaths, removed: removedIndexPaths)
        }
        
        let layout = FlexiCollectionViewLayout()
        collectionView?.collectionViewLayout = layout
    
    question 
    opened by chepil 6
  • Using Preheat

    Using Preheat

    Hi Alexander,

    I'm interested in using Preheat for my project to load very deep view structures before I need them. I was looking at the code a bit, my hesitation is that Preheat may not be tool I'm looking for. Can you speak to its performance for doing that kind of preloading rather than just purely images?

    Thanks! Zack

    opened by zackshapiro 5
  • Fast scrolling in PreheaterController showing no images even images already downloaded

    Fast scrolling in PreheaterController showing no images even images already downloaded

    Hi kean i am using UITableView as PreheaterController, and nuke to download images. Images are approximately 2 MB each. Issue is that after downloading all images if i am scrolling little bit fast (more then normal scrolling by a user) image view not showing respected image. Its working great on normal scrolling. Is it due to preheat method which call nuke to stopPreheatingImages ?

    Thanks.

    opened by raviatri 4
  • How to use Preheat with swift 2.3

    How to use Preheat with swift 2.3

    Hello kean thanks for this great library i tested it on swift 3.0 and it is great library great experience. But i have to integrate this with my project written in swift 2.3 and i tried a lot but failed. Is it possible to use this with swift 2.3?

    Thanks

    question 
    opened by raviatri 2
  • Manual Installation

    Manual Installation

    CocoaPods and Carthage are awesome tools and make our life really easier, but there are some devs who still don't know how to use them.

    It would be cool to add the Manual installation guide in your README.md. You can take a look at my iOS Readme Template to see how you can do it.

    opened by lfarah 2
  • What is

    What is "PreheatControllerDelegate"?

    I saw in your example, there is a PreheatControllerDelegate.

    After I installed Preheat, cleaned and build, the compiler shows PreheatControllerDelegate is undeclared. (I have import Preheat)

    And I can't find the documentation about it, what is it and how to fix this?

    opened by joe528 1
Releases(0.4.1)
Owner
Alexander Grebenyuk
I write kean.blog and like porridge
Alexander Grebenyuk
Netflix and App Store like UITableView with UICollectionView, written in pure Swift 4.2

GLTableCollectionView Branch Status master develop What it is GLTableCollectionView is a ready to use UITableViewController with a UICollectionView fo

Giulio 708 Nov 17, 2022
Incremental update tool to UITableView and UICollectionView

EditDistance is one of the incremental update tool for UITableView and UICollectionView. The followings show how this library update UI. They generate

Kazuhiro Hayashi 90 Jun 9, 2022
🚴 A declarative library for building component-based user interfaces in UITableView and UICollectionView.

A declarative library for building component-based user interfaces in UITableView and UICollectionView. Declarative Component-Based Non-Destructive Pr

Ryo Aoyama 1.2k Jan 5, 2023
A generic small reusable components for data source implementation for UITableView/UICollectionView in Swift.

GenericDataSource A generic small reusable components for data source implementation for UITableView/UICollectionView written in Swift. Features Basic

null 132 Sep 8, 2021
ZHTCView - UITableview & UICollectionView

ZHTCView 这是一个使用Block替换代理的UITableview & UICollectionView。 使用方法如下: - (DSTableView *)tableView { if (!_tableView) { _tableView = DSTableView.

黑酒一 0 Jan 10, 2022
💾 A library for backporting UITableView/UICollectionViewDiffableDataSource.

DiffableDataSources ?? A library for backporting UITableView/UICollectionViewDiffableDataSource powered by DifferenceKit. Made with ❤️ by Ryo Aoyama I

Ryo Aoyama 762 Dec 28, 2022
Easier way to represent the structure of UITableView.

Shoyu Shoyu is a library written in Swift to represent UITableView data structures. Shoyu means Soy Sauce in Japanese. Usage Create single section and

yukiasai 278 Apr 14, 2022
A data-driven UICollectionView framework for building fast and flexible lists.

A data-driven UICollectionView framework for building fast and flexible lists. Main Features ?? Never call performBatchUpdates(_:, completion:) or rel

Instagram 12.5k Jan 1, 2023
Collapse and expand UICollectionView sections with one method call.

This library provides a custom UICollectionView that allows to expand and collapse sections. Provides a simple API to manage collection view appearanc

Touchlane 172 Dec 26, 2022
Protocol-oriented UICollectionView management, powered by generics and associated types.

DTCollectionViewManager Features Powerful mapping system between data models and cells, headers and footers Automatic datasource and interface synchro

Denys Telezhkin 308 Jan 6, 2023
UICollectionView layout for presenting of the overlapping cells.

StickyCollectionView UICollectionView layout for presenting of the overlapping cells. Objective-C version here Checkout demo Overview Installation Man

Bogdan Matveev 325 Oct 11, 2022
Reimagining UICollectionView

CollectionKit Reimagining UICollectionView A modern Swift framework for building composable data-driven collection view. Migration Guide v2.0 Features

SoySauceLab 4.3k Dec 27, 2022
Conv smart represent UICollectionView data structure more than UIKit.

Conv Conv smart represent UICollectionView data structure more than UIKit. Easy definition for UICollectionView DataSource and Delegate methods. And C

bannzai 157 Nov 25, 2022
CollectionView - UICollectionView using UICollectionViewCompositionalLayout

CollectionView UICollectionView using UICollectionViewCompositionalLayout create

null 0 Jan 11, 2022
CollectionViewSegmentedControl - Scrollable UISegmentedControl built using a UICollectionView

CollectionViewSegmentedControl Installation CocoaPods Download CocoaPods Run 'Po

James Sedlacek 7 Nov 24, 2022
Conv smart represent UICollectionView data structure more than UIKit.

Conv Conv smart represent UICollectionView data structure more than UIKit. Easy definition for UICollectionView DataSource and Delegate methods. And C

bannzai 155 May 12, 2022
A modest attempt to port UICollectionView to SwiftUI.

LazyCollectionView A modest attempt to port UICollectionView to SwiftUI. Table of Contents Description Requirements Installation Usage Components Impr

Unsplash 109 Dec 27, 2022
PJFDataSource is a small library that provides a simple, clean architecture for your app to manage its data sources while providing a consistent user interface for common content states (i.e. loading, loaded, empty, and error).

PJFDataSource PJFDataSource is a small library that provides a simple, clean architecture for your app to manage its data sources while providing a co

Square 88 Jun 30, 2022
Easy and type-safe iOS table and collection views in Swift.

Quick Start TL;DR? SimpleSource is a library that lets you populate and update table views and collection views with ease. It gives you fully typed cl

Squarespace 96 Dec 26, 2022