Skip to content
This repository has been archived by the owner on Apr 18, 2019. It is now read-only.
/ DTStorage Public archive

A library for data persistence on iOS that uses SQLite (with FMDB)

License

Notifications You must be signed in to change notification settings

diogot/DTStorage

Repository files navigation

Build Status

DTStorage

A library for data persistence on iOS that uses SQLite (with FMDB). I (Diogo Tridapalli) started to develop this lib for the iOS app of 99Taxis. Still is under development but it's functional.

A bit of History

To write this code I was inspired by Brent Simmons in his talk at AltWWDC'13 and in a nice article in his blog. After that I studied a bit of FMDB and started to write a little layer over FMDB to avoid duplicated SQL code.

After a few interactions on code (and some spaghetti code) I decide do some research to see what the people are doing on this matter and I found another great article from Simmons. And a similar library from Marco Arment, FCModel has nice ideas that I use in this code, like the way used to open the database, but this code have support to simple relations, that FCModel don't.

Since most of the data persistence libraries are based on Core Data I, with the support of 99Taxis, decided to open this source code. I hope it can help :-)

Suggestions are welcome, feel free to contact me on twitter.

Requirements

  • iOS 6 or later
  • ARQ
  • FMDB

Example

CocoaPods config

pod 'DTStorage'

Object definition

#import "DTSObject.h"
@interface ObjectExample : DTSObject
@property (nonatomic, strong) NSString *string;
@property (nonatomic, strong) NSNumber *number;
@end

@implementation ObjectExample
+ (NSDictionary *)propertiesTypes
{
    return @{@"string": NSStringFromClass([NSString class]),
             @"number": NSStringFromClass([NSNumber class])};
}
+ (NSString *)tableName
{
    return @"example";
}
@end

DTSManager configuration

DTSManager *manager = [DTSManager sharedManager];
[manager addManagedClass:[ObjectExample class]];
[manager openDataBaseAtPath:dbPath
                 withSchema:^(FMDatabase *db, int *schemaVersion) {
  [db beginTransaction];
  void (^failedAt)(int statement) = ^(int statement){
    int lastErrorCode = db.lastErrorCode;
    NSString *lastErrorMessage = db.lastErrorMessage;
    [db rollback];
    NSAssert3(0, @"Migration statement %d failed, code %d: %@", statement, lastErrorCode, lastErrorMessage);
  };
  if (*schemaVersion < 1) {
    if (! [db executeUpdate:
      @"CREATE TABLE example ("
      @" objectId INTEGER UNIQUE NOT NULL PRIMARY KEY AUTOINCREMENT,"
      @" string   TEXT NOT NULL DEFAULT '',"
      @" number   REAL"
      @");"
    ]) failedAt(1);
        
    *schemaVersion = 1;
  }
  [db commit];
}];

Object creation

ObjectExample *obj = [ObjectExample new];
obj.string = @"text";
obj.number = @(42);
[obj save];

Object list and load

NSArray *list = [ObjectExample arrayWithObjectIds];
ObjectExample *obj = [ObjectExample objectWithId:[list lastObject]];

Delete

[obj delete];

About

A library for data persistence on iOS that uses SQLite (with FMDB)

Resources

License

Stars

Watchers

Forks

Packages

No packages published