Coder Social home page Coder Social logo

slidingwindow's Introduction

SlidingWindow

Data loading in a sliding window fashion. Specifically for using with raw SQLite databases.

Problem

SQLite is attractive. Core Data is there, Realm is there. But using raw SQLite still has its charm. However, other than persistence, SQLite doesn't help you in how you manage data you copied to use in your app. So, for really long lists of data, you have to engineer how you're going to have that data loaded fast and without hogging memory.

Idea

Solution: Pagination. However, its simple (and popular) form of it is still problematic. It goes like this, fetch your data "page by page" using LIMIT <skip>, <count>, and append every page to an array. Obviously this is not memory-efficient because that array keeps growing. We want to only keep a few pages in memory, while those who are far away can have their memory reclaimed. This is the essence of a sliding window data loading technique.

So, this how things work in general:

  1. First, you have to provide all the ids of the records beforehand. This should not use much memory, and also should be much quicker since there's no copying being done.
  2. Initialize the window with the ids, window size (optional), and a closure to provide record data corresponding to ids of the requested window.
  3. Now the window begins using the provided closure to fetch items of the current window. This is done as soon as the first item in the said window is requested. Of course this is done asynchronously. However, you can optionally block the current thread till the data is ready. This may be useful for table views with self sizing cells that depends on the expected data. See the example.
  4. When the data of the current window is fetched, it's then put in a memory cache. This cache is configurable to retain a number of windows. It gets rid of items in pages further than the current page with a configurable threshold.
  5. A callback can optionally be called to reload data upon it becoming ready.

See example project for usage.

slidingwindow's People

Contributors

ahmedk92 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.