GithubHelp home page GithubHelp logo

apsmartstorage's Introduction

APSmartStorage helps to get data from network and automatically caches data on disk or in memory in a smart configurable way. Here is how the APSmartStorage flow diagram looks like: Build Status

Features

  • Load cached object from memory by URL
  • Load cached object from file by URL
  • Load object from network by URL
  • Store loaded object to file
  • Store loaded object to memory
  • Parse loaded data from network (for instance, NSData to UIImage)
  • Automatically purge memory cache on memory warning
  • Set max object count to keep in memory to prevent memory overflow
  • Set custom NSURLSessionConfiguration

Installation

Add APSmartStorage pod to Podfile

Using

Example

Here is example how to use APSmartStorage to store images

// setup data parsing block
APSmartStorage.sharedInstance.parsingBlock = ^(NSData *data, NSURL *url)
{
    return [UIImage imageWithData:data scale:UIScreen.mainScreen.scale];
};
...
// show some progress/activity
...
// load object with URL
[APSmartStorage.sharedInstance loadObjectWithURL:imageURL callback:(id object, NSError *error)
{
    // hide progress/activity
    if (error)
    {
        // show error
    }
    else 
    {
        // do something with object
    }
}];
Update stored object

Objects stored at files could become outdated after some time, and application should reload it from network

[APSmartStorage.sharedInstance reloadObjectWithURL:objectURL callback:(id object, NSError *error)
{
    if (error)
    {
        // show error and do something like use outdated version of object
    }
    else
    {
        // do something with object
    }
}];
Remove objects from memory
// remove object from memory
[APSmartStorage.sharedInstance removeObjectWithURLFromMemory:objectURL];
// remove all objects form memory
[APSmartStorage.sharedInstance removeAllFromMemory];
Remove object from memory and delete file storage
// remove object from memory and remove it file
[APSmartStorage.sharedInstance removeObjectWithURLFromStorage:objectURL];
// remove all objects from memory and all storage files
[APSmartStorage.sharedInstance removeAllFromStorage];

This methods also cancel network requests. And if request has been cancelled, callbacks will run with nil as object and with error with code 701.

Parse network and file data

If parsingBlock doesn't set you will receive raw NSData downloaded from network or read from file. So you should set one in most cases. If you need to parse data of different formats it will be a bit more complicated:

APSmartStorage.sharedInstance.parsingBlock = ^(NSData *data, NSURL *url)
{
    // is URL of image
    if ([url isImageURL])
    {
        return [UIImage imageWithData:data scale:UIScreen.mainScreen.scale];
    }
    // this is URL of something else
    else
    {
        return [SomeObject objectWithData:data];
    }
};
Set max objects count to keep in memory

If max object count reached random object will be removed from memory before add next one

APSmartStorage.sharedInstance.maxObjectCount = 10;
Setup custom NSURLSessionConfiguration
APSmartStorage.sharedInstance.sessionConfiguration = sessionConfiguration;

History

Version 0.1.0

  • Added cancel network requests on object remove
  • Renamed methods

Version 0.0.7

  • Improved callbacks to run immediately if object found in memory storage

Version 0.0.6

  • Fixed crash on 'nil' object set to memory storage

Version 0.0.5

  • Public release

======================= githalytics.com alpha If you have improvements or concerns, feel free to post an issue and write details.

Check out all Alterplay's GitHub projects. Email us with other ideas and projects.

apsmartstorage's People

Contributors

belkevich avatar slavabushtruk avatar

Stargazers

 avatar

Watchers

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