Skip to content

darkarmyIN/DADataManager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DADataManager

Lightweight storage library for iOS.


Ever needed to save a singular URL response, for testing purposes and didn't want to use a persistancy framework like CoreData or Realm; Or a rarely subject-to-change static response that you probably saved in NSUserDefaults; Or just wanted to save a downloaded file for later retrieval.

Enter DADataManager to the rescue.


Installation

CocoaPods (recommended)

  • Add pod 'DADataManager' to your podfile.
  • Add #import <DADataManager/DADataManager.h> (or @import DADataManager if you build as a framework/Swift) in the class you want to use it.

Manual (not recommended)

  • Clone the repository somwhere.
  • Drop the folder DADataManager into your project.
  • Add #import "DADataManager.h" in the class you want to use it.

Usage

Create a instance of the DADataManager class.
DADataManager *manager = [DADataManager sharedManager];
let manager = DADataManager.sharedManager()
Getting paths.

[manager imagesPathForFileName:@"image.jpg"] (image stored in ../Documents/Data/images)

[manager dataFilesPathForFileName:@"file.json"] (file stored in ../Documents/Data/files)

[manager rootPathForFileName:@"MyApp/Media/Videos/01.mp4"] (file stored in ../MyApp/Media/Videos/01.mp4)

manager.libraryPathForFileName:("Samaritan/Core/Neural Nets/6741.SMRTN") (file stored at path ../Library/Samaritan/Core/Neural Nets/6741.SMRTN)

manager.videosURLForFileName("1802.MP4") (File URL for video at path ../Documents/Data/vidoes/1802.MP4)

Saving objects.
NSArray *array = @[@"One", @"Two", @"Three"];
[manager saveObject:array toFilePath:[manager documentsPathForFileName:@"array.json"]];
let object = ["foo": 1, "bar": 2, "poo": 3, "jar": 4]
manager.saveObject(object, toFilePath: manager.dataFilesPathForFileName("file.dat"))
Saving data downloaded off the internet
NSURLSession.sharedSession().dataTaskWithURL(URL, completionHandler: { (data, response, error) in
	// Other stuff
	let filePath = manager.videosPathForFileName("video.mov")
	manager.saveData(data, toFilePath: filePath)
}).resume()
Saving and getting images
let image = UIImage(data: data)
dataManager.saveImage(image, fileName: manager.imagesPathForFileName("pic.jpg"))
		
let newImage = manager.getImageWithFileName("pic.jpg")
Deleting files
NSString *filePath = [manager documentsPathForFileName:@"array.json"];
[manager deleteFileAtFilePath:filePath]

Methods

Paths for file names

These methods return a file path or URL for files stored in the application's documents or library folder.

 - (NSString *)documentsPathForFileName:(NSString *)fileName;
 - (NSString *)libraryPathForFileName:(NSString *)fileName;

Get the file paths for sub directories inside the documents folder.

 - (NSString *)dataFilesPathForFileName:(NSString *)fileName;
 - (NSString *)imagesPathForFileName:(NSString *)fileName;
 - (NSString *)audioPathForFileName:(NSString *)fileName;
 - (NSString *)videosPathForFileName:(NSString *)fileName;
 - (NSURL *)videosURLForFileName:(NSString *)fileName;
Check if a file exists in the documents.
- (BOOL)fileExistsInDocuments:(NSString *)fileName;
- (BOOL)dataFileExistsInDocuments:(NSString *)fileName;
- (BOOL)imageExistsInDocuments:(NSString *)fileName;
- (BOOL)audioFileExistsInDocuments:(NSString *)fileName;
- (BOOL)videoExistsInDocuments:(NSString *)fileName;
- (BOOL)videoURLExistsInDocuments:(NSURL *)url;
Delete files at a file path or URL.
- (BOOL)deleteFileAtFilePath:(NSString *)filePath;
- (BOOL)deleteFileAtFileURL:(NSURL *)url;
Data utilities.
- (BOOL)saveData:(NSData *)data toFilePath:(NSString *)filePath;
- (BOOL)saveData:(NSData *)data toDocumentsFile:(NSString *)fileName;
- (BOOL)saveObject:(id)object toFilePath:(NSString *)filePath;
- (BOOL)saveObject:(id)object toDocumentsFile:(NSString *)fileName;
- (id)fetchJSONFromDocumentsFilePath:(NSString *)filePath;
- (id)fetchJSONFromDocumentsFilepath:(NSString *)filePath error:(NSError **)error;
- (id)fetchJSONFromDocumentsDataFileName:(NSString *)fileName;

Image utilities.

- (BOOL)saveImage:(UIImage *)image fileName:(NSString *)fileName;
- (UIImage *)getImageWithFileName:(NSString *)fileName;
Shared instance
+ (DADataManager)sharedManager;

About

Lightweight storage library for iOS.

Resources

License

Stars

Watchers

Forks

Packages

No packages published