Persistent Objects for Cocoa & Cocoa Touch that using SQLite.
This project based on http://code.google.com/p/sqlitepersistentobjects and https://bitbucket.org/gabrielayuso/sqlitepersistentobjects (make it thread safely)
- Add
SQLitePersistentObjects.framework
to your Xcode project - Link the
libsqlite3.dylib
- Add
-ObjC
to your project's settingsOther Linker Flags
#import <SQLitePersistentObjects/SQLitePersistentObjects.h>
and subclassSQLitePersistentObject
for your data model
Overview: http://code.google.com/p/sqlitepersistentobjects/source/browse/trunk/ReadMe.txt
-
Declare your data objects inherited from
SQLitePersistentObject
. Every property that's not a collection class (NSDictionary, NSArray, NSSet or mutable variants) will get persisted into a column in the database. -
Name your properties in the lower camel case way. e.g.
productName
will be stored in datebase by namedproduct_name
. -
Send
- (void)save;
method to save the data object to database. -
Query:
// get all Product objects from database NSArray *allObjects = [Product allObjects]; // query with sql, sql starts with 'WHERE' Product *aProduct = (Product *)[Product findFirstByCriteria:@"WHERE pid = 100"]; NSArray *someProducts = [Product findByCriteria:@"WHERE price > 20.0 LIMIT 0,30"];
-
Indexes: just override
+ (NSArray *)indices;
class method.+ (NSArray *)indices { return [NSArray arrayWithObjects: [NSArray arrayWithObjects:@"pID", nil], [NSArray arrayWithObjects:@"price", nil] , nil]; }
-
To filter properties that does not need to be saved to the database, just override
+ (NSArray *)transients;
class method.+ (NSArray *)transients { return [NSArray arrayWithObjects:@"isNewlyAdded", nil]; }
-
- (void)deleteObject;
-
+ (void)clearCache;
-
[SQLiteInstanceManager reset];
-
[[SQLiteInstanceManager sharedManager] setDatabaseFilepath:dbPath];
;
You may check out the file SQLitePersistentObject.h
or the SPOSample project for more.