Permalink
Browse files

locations now written to and read from app's local database

  • Loading branch information...
1 parent 5a0cbe7 commit 988925eed220e9988a72e662ad97785f9464239c @natevw committed Apr 26, 2012
@@ -26,6 +26,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.logger = [TLTrailer new];
+ NSLog(@"Current sequence: %u", self.logger.currentSequence);
+ NSLog(@"%@", [self.logger updatesSince:0]);
+ [self.logger removeUpdatesThrough:self.logger.currentSequence];
+
return YES;
}
@@ -9,15 +9,19 @@
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
+#include <sqlite3.h>
typedef NSUInteger TLTrailerSequence;
@interface TLTrailer : NSObject <CLLocationManagerDelegate>
-@property (strong, nonatomic) CLLocationManager *locManager;
+@property (strong, readonly) CLLocationManager *locManager;
+@property (readonly) sqlite3* db;
+@property (readonly) sqlite3_stmt* insertLoc;
+
@property (readonly) TLTrailerSequence currentSequence;
-- (NSArray*)updatesUntil:(TLTrailerSequence)seq;
-- (void)removeUpdates:(TLTrailerSequence)seq;
+- (NSArray*)updatesSince:(TLTrailerSequence)seq;
+- (void)removeUpdatesThrough:(TLTrailerSequence)seq;
@end
@@ -7,51 +7,80 @@
//
#import "TLTrailer.h"
-#include <sqlite3.h>
+
@implementation TLTrailer
@synthesize locManager = _locManager;
@synthesize currentSequence = _currentSequence;
+@synthesize db = _db;
+@synthesize insertLoc = _insertLoc;
- (id)init {
NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"trailer-v1.sqlite3"];
-
- sqlite3* db;
- sqlite3_open([databasePath fileSystemRepresentation], &db);
- sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS location_logs(data TEXT)", NULL, NULL, NULL);
+
+ sqlite3_open([databasePath fileSystemRepresentation], &_db);
+ sqlite3_exec(self.db, "CREATE TABLE IF NOT EXISTS location_logs(id INTEGER PRIMARY KEY AUTOINCREMENT, data BLOB)", NULL, NULL, NULL);
// fetch latest sequence id, thanks http://stackoverflow.com/a/5301923/179583
sqlite3_stmt* q;
- sqlite3_prepare_v2(db, "SELECT seq FROM SQLITE_SEQUENCE WHERE name='location_logs'", -1, &q, NULL);
+ sqlite3_prepare_v2(self.db, "SELECT seq FROM SQLITE_SEQUENCE WHERE name='location_logs'", -1, &q, NULL);
sqlite3_step(q);
- _currentSequence = sqlite3_column_int(q, 0);
+ _currentSequence = sqlite3_column_int64(q, 0);
sqlite3_finalize(q);
- // TODO: keep this handy for later instead...
- sqlite3_close(db);
+ sqlite3_prepare_v2(self.db, "INSERT INTO location_logs (data) VALUES (?)", -1, &_insertLoc, NULL);
// TODO: use NSUserDefaults to store/re-set last selected states
- self.locManager = [CLLocationManager new];
+ _locManager = [CLLocationManager new];
self.locManager.delegate = self;
self.locManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locManager startUpdatingLocation];
return self;
}
+- (void)dealloc {
+ sqlite3_finalize(self.insertLoc);
+ sqlite3_close(self.db);
+}
+
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Error: %@", error);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
- NSLog(@"Update: %@", newLocation);
+ NSData* locData = [NSKeyedArchiver archivedDataWithRootObject:newLocation];
+ sqlite3_bind_blob(self.insertLoc, 1, [locData bytes], [locData length], SQLITE_STATIC);
+ sqlite3_step(self.insertLoc);
+ sqlite3_reset(self.insertLoc);
+ sqlite3_clear_bindings(self.insertLoc);
+ [locData self]; // make sure data retained until insertion is done
+ _currentSequence = sqlite3_last_insert_rowid(self.db);
}
-- (NSArray*)updatesUntil:(TLTrailerSequence)seq { return nil; }
-- (void)removeUpdates:(TLTrailerSequence)seq {}
+- (NSArray*)updatesSince:(TLTrailerSequence)seq {
+ sqlite3_stmt* q;
+ sqlite3_prepare_v2(self.db, "SELECT data FROM location_logs WHERE id > ?", -1, &q, NULL);
+ sqlite3_bind_int64(q, 1, seq);
+
+ NSMutableArray* updates = [NSMutableArray array];
+ while (sqlite3_step(q) == SQLITE_ROW) {
+ NSData* locArchive = [NSData dataWithBytesNoCopy:(void*)sqlite3_column_blob(q, 0)
+ length:sqlite3_column_bytes(q, 0)
+ freeWhenDone:NO];
+ CLLocation* loc = [NSKeyedUnarchiver unarchiveObjectWithData:locArchive];
+ [updates addObject:loc];
+ }
+ return updates;
+}
+- (void)removeUpdatesThrough:(TLTrailerSequence)seq {
+ char* sql = sqlite3_mprintf("DELETE FROM location_logs WHERE id <= %i", seq);
+ sqlite3_exec(self.db, sql, NULL, NULL, NULL);
+ sqlite3_free(sql);
+}
@end

0 comments on commit 988925e

Please sign in to comment.