Giter Site home page Giter Site logo

data-loader's Introduction

data-loader

Loading data from multiple sources with ease

Quality Gate Quality Gate Quality Gate Quality Gate

Usage

Including In Your Project

Add maven url in repositories:

maven { url 'http://maven.android-forever.com/' }

Add the library to your dependencies:

Android

compile 'com.androidforever:data-loader-android:2.1.2'

iOS

  • Setup j2objc
  • Include classes from ios_classes to your project

Example

Create new DataLoader

To support j2objc, DataLoader is abstract class. Method runOnUiThread should be implemented On android you can use AndroidDataLoader subclass On iOS use DataLoader.h

Android

DataLoader<T> dataLoader = new AndroidDataLoader();

iOS

TDataLoader *dataLoader = [[DataLoaderImpl alloc] init];

Create DataProvider

DataProvider should load data from single source. For example you can have NetworkDataProvider which loads data from some API, DatabaseDataProvider that loads data from database and so on.

You add a list of DataProviders to DataLoader in order in which you want to load your data. DataLoader will load data from providers in order, until one of the providers returns true in boolean load()

For example you can add NetworkDataProvider as first provider and DatabaseDataProvider as second. This way, if for NetworkDataProvider fails for some reason, DataLoader will then try the next provider, in this case DatabaseDataProvider

To create DataProvider you need to implement following methods from DataProvider interface

  • boolean load()
    This is the main method for loading data. You should load your data here
    This method is called on worker thread so you can freely make api calls here.
    Return value should indicate if loading was successful or not. It is required to return valid value here to indicate to DataLoader if this provider succeeded or not

  • T getResult()
    Return your loading result here

  • boolean forceLoading()
    If this method returns true, DataLoader will load data from this provider event if one of the previous providers loaded data successfully

Add providers to DataLoader

You can add DataProvider to DataLoader in two ways

Set a list of providers

List<DataProvider> providers = new ArrayList<>();
dataLoader.setProvider(providers);

Add provider to DataLoader directly

dataLoader.addProvider(networkProvider);
dataLoader.addProvider(databaseprovider);

Callback

You can set callback

dataLoader.setListener(new DataLoader.LoadListener()
{
   @Override
   public void onLoadingFinished(int status)
   {

   }

   @Override
   public void onDataLoaded(DataLoader.Result result)
   {

   }

   @Override
   public void onLoadStarted()
   {

   }
})
  • void onLoadStarted()
    Called when DataLoader has started loading data

  • onLoadingFinished(int status)
    Called when DataLoader has finished loading data

  • onDataLoaded(DataLoader.Result result)
    Called when data has been loaded from single DataProvider
    use result.data to access loaded data, returned from getResult
    use result.provider to access DataProvider that loaded this data
    use result.status to check if loading was successful or not

Start loading

You can start loading data by using loadData

  • To load data asynchronously use:
dataLoader.loadData()

Result will be delivered in callback if you set one

  • To load synchronously use:
DataLoader.Result result = dataLoader.loadData(true)

Developed By

License

Copyright 2014 Predrag Čokulov

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

data-loader's People

Contributors

bataemperor 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.