Skip to content

Latest commit

 

History

History
 
 

SQLiteStack

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

SQLiteStack

A simple SQLite Stack

[![CI Status](http://img.shields.io/travis/Khoa Pham/SQLiteStack.svg?style=flat)](https://travis-ci.org/Khoa Pham/SQLiteStack) Version License Platform

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

How it works

  • Prefer protocol over inheritance via FTGSQLiteSerializing
  • Inspect model properties via FTGModelHelper
  • Convert model into SQLite statements via FTGSQLiteHelper
  • Create tables, execute statements via FTGSQLiteStack

Example

Model

#import <Foundation/Foundation.h>
#import "FTGSQLiteSerializing.h"

@interface FTGNote : NSObject <FTGSQLiteSerializing>

@property (nonatomic, copy) NSString *modelID;
@property (nonatomic, copy) NSString *content;
@property (nonatomic) NSTimeInterval timeInterval;

@end
@implementation FTGNote

+ (NSArray<NSString *> *)propertyKeys {
    return @[@"modelID",
             @"content",
             @"timeInterval",
             ];
}

@end

DataManager

@implementation FTGDataManager

- (FTGNote *)createNoteWithContent:(NSString *)content {
    FTGNote *note = [[FTGNote alloc] init];
    note.modelID = [[NSUUID UUID] UUIDString];
    note.content = content;
    note.timeInterval = [[NSDate date] timeIntervalSince1970];

    return note;
}

- (void)saveNotes:(NSArray<FTGNote *> *)notes {
    FTGSQLiteHelper *helper = [FTGSQLiteHelper new];
    NSArray<NSString *> *modifiedStatements = [notes map:^id(FTGNote *note) {
        return [helper insertOrUpdateStatementForModel:note modelClass:[FTGNote class]];
    }];

    [self.stack executeStatements:modifiedStatements];
}

- (NSArray<FTGNote *> *)loadNotes {
    return (id)[self.stack loadAllWithModelClass:[FTGNote class]];
}

@end

Installation

SQLiteStack is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SQLiteStack"

Author

Khoa Pham, [email protected]

License

SQLiteStack is available under the MIT license. See the LICENSE file for more info.