Iron


Source link: https://github.com/FabianTerhorst/Iron

Iron

Fast and easy to use NoSQL data storage with RxJava and Kotlin support

Android sdk version 8 support

Add dependency

Add apt plugin in your top level gradle build file

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

Apply apt plugin in your application gradle build file.

apply plugin: 'com.neenbedankt.android-apt'

Add dependencies to your application gradle build file

The Core

compile 'io.fabianterhorst:iron:0.8.0'

The Extensions

compile 'io.fabianterhorst:iron-retrofit:0.8.0' compile 'io.fabianterhorst:iron-encryption:0.8.0' //is only required for using the compiler compile 'io.fabianterhorst:iron-annotations:0.8.0' apt 'io.fabianterhorst:iron-compiler:0.8.0'

Initiate Iron instance with application context

public class MyApplication extends Application {

  @Override
  public void onCreate() {

super.onCreate();

Iron.init(getApplicationContext());

Iron.setCache(Cache.MEMORY);
//default is NONE

//Optional if iron-retrofit is included

Iron.setLoader(new IronRetrofit());

//Optional if iron-encryption is included

Iron.setEncryption(new IronEncryption());

  
}
 
}

Use the @Store annotation on any plain old Java object.

@Store public class Main {

  @DefaultObject
  @Name(value = "contributor_list", transaction = true, listener = true, loader = true, async = true)
  ArrayList<Contributor> contributors;

String userName;

@DefaultLong(120)
  Long myLong; 
}

Now you can access the generated Methods from your Main + "Store" file.

 MainStore.setContributors(contributors);

Get value synced

 MainStore.getContributors(contributors);

Get value asynced

MainStore.getContributors(new Chest.ReadCallback<ArrayList<Contributor>>() {

  @Override
  public void onResult(ArrayList<Contributor> contributors) {

  
}
 
}
);

Remove value

 MainStore.removeContributors();
 Iron.chest().addOnDataChangeListener(new DataChangeCallback(this) {

  @Override
  public void onDataChange(String key, Object value) {

if(key.equals(MainStore.Keys.CONTRIBUTORS.toString())){

 Log.d(TAG, ((ArrayList<Contributor>)value).toString());

}

  
}
 
}
);

transactions (changes will be saved)

 MainStore.executeContributorsTransaction(new Chest.Transaction<ArrayList<Contributor>>() {

  @Override
  public void execute(ArrayList<Contributor> contributors) {

Contributor contributor = new Contributor();

contributor.setName("fabianterhorst");

contributors.add(contributor);

  
}
 
}
);

Use a internal transaction to add a object to the list and save it automatically

Contributor contributor = new Contributor();
 contributor.setName("test");
 MainStore.addContributor(contributor);

Use a internal transaction to add objects to the list and save it automatically

ArrayList<Contributor> contributors = new ArrayList<>();
 for(int i = 0;i < 10;i++){

  Contributor contributor = new Contributor();

  contributor.setName("name" + i);

  contributors.add(contributor);
 
}
 MainStore.addContributors(contributors);

Data change listener

MainStore.addOnContributorsDataChangeListener(new DataChangeCallback<ArrayList<Contributor>>(this) {

  @Override
  public void onDataChange(ArrayList<Contributor> value) {

  
}
 
}
);

Generic data change listener

MainStore.addOnDataChangeListener(new DataChangeCallback(this) {

  @Override
  public void onDataChange(String key, Object value) {

if(key.equals(MainStore.Keys.CONTRIBUTORS.toString()))

 //contributors changed
  
}
 
}
);

Search for object with field and value

MainStore.getContributorsForField("login", "fabianterhorst", new Chest.ReadCallback<Contributor>() {

  @Override
  public void onResult(Contributor contributor) {

if(contributor != null)

 Log.d(TAG, contributor.toString());

  
}
 
}
);

RxJava support

Set a value asynchron with RxJava

Iron.chest().set("name", "Fabian");

Get a value asynchron with RxJava

Iron.chest().<String>get("name").compose(this.<String>bindToLifecycle()).subscribe(new Subscriber<String>() {

  @Override

 public void onError(Throwable e) {

  e.printStackTrace();

 
}

  @Override

 public void onNext(String s) {

  //called when name changed and when name was loaded asynchron

  Log.d("name", s);

 
}

  @Override

 public void onCompleted() {

  
}

}
);

With RxJava the Retrofit extension isn´t needed anymore.

Observable<List<Repo>> reposCallObservable = service.listReposRxJava("fabianterhorst");
 Iron.chest().load(reposCallObservable, Repo.class);

You can also use Iron.chest()´s methods.

Iron.chest().write("username", "fabian");

Read data objects. Iron instantiates exactly the classes which has been used in saved data. The limited backward and forward compatibility is supported.

String username = Iron.chest().read("username");

Laod and save data with retrofit. Need loader extension to be added in application.

Call<List<Repo>> reposCall = service.listRepos("fabianterhorst");
 Iron.chest().load(reposCall, Repo.class);

Get value asynced with default value

Iron.chest().get("contributors", new Chest.ReadCallback<ArrayList<Contributor>>() {

  @Override
  public void onResult(ArrayList<Contributor> contributors) {

  
}
 
}
, new ArrayList<Contributor>());

Remove listener to prevent memory leaks

@Override protected void onDestroy() {

  super.onDestroy();

  MainStore.removeListener(this);

  //Iron.chest().removeListener(this);
 
}

Handle data structure changes

Class fields which has been removed will be ignored on restore and new fields will have their default values. For example, if you have following data class saved in Paper storage:

class User {

  public String name; //Fabian
  public boolean isActive; 
}

And then you realized you need to change the class like:

class User {

  public String name; //Fabian
  // public boolean isActive; removed field
  public Location location; // New field 
}

Then on restore the isActive field will be ignored and new location field will have its default value null.

Retrofit support

Call<List<Contributor>> userCall = service.contributors("fabianterhorst", "iron");
 MainStore.loadContributors(userCall);

with Cache:

Running Benchmark on Nexus 6p, in ms:

Benchmark Iron Hawk sqlite
Read/write 500 contacts 29 142
Write 500 contacts 27 60
Read 500 contacts 0 63

Running Benchmark on Emulator, in ms:

Benchmark Iron Hawk sqlite
Read/write 500 contacts 12 54
Write 500 contacts 11 25
Read 500 contacts 0 24

with Encryption (only in Iron):

Running Benchmark on Nexus 6p, in ms:

Benchmark Iron Hawk sqlite
Read/write 500 contacts 53 142
Write 500 contacts 28 61
Read 500 contacts 23 63

Snapshot Builds

Add the jitpack repository in your root build.gradle at the end of repositories

allprojects {

  repositories {

maven {
 url "https://jitpack.io" 
}

  
}
 
}
//Latest commit compile 'com.github.FabianTerhorst:Iron:-SNAPSHOT'

License

Copyright 2016 Fabian Terhorst  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. 

Resources

This is a lightweight Logger to file for Android. Logging to file is a common task and there is no good solution to do it effectively. This library does it for you.

Swipe-able view, which allows users to perform actions by swiping it to the left or right side.

Open Weather API Wrapper is an Android wrapper for the APIs of https://openweathermap.org. This library handles all the network operations and parameter validations on behalf of the developer.

A fast annotation processor to make your objects Parcelable without writing any of the boilerplate.

A small customizable library useful to handle an gallery image pick action built-in your app.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes