Natasha Tenn

unread,

Apr 12, 2010, 6:15:48 PM4/12/10

to thr...@googlegroups.com

Does anyone have any examples using SQLite as a its datasource?  I've searched through the threads and see that it can be done, but I learn so much from the experts that have done it.  Basically, I need to 'mimic and replace the TTURLRequests with my SQL queries', correct?

Thanks.

uprise78

unread,

Apr 12, 2010, 7:25:48 PM4/12/10

to Three20

This is really easy. Just look at TTURLRequestModel. Create a
TTDBAccessModel and copy/paste the code in there. Now you have a
choice: do you want async access to your DB or synchronous? If
async, you once again can just mimic a Three20 class: TTURLRequest.
Mimic it replacing the URL requests with DB queries. If you want
synchronous access you are already done.

mamcx

unread,

Apr 12, 2010, 7:37:35 PM4/12/10

to Three20

> This is really easy.

I'm missing something big, because my attemps have failed. Using async
mean big issues with threading problems and not exist any usable
thread-safe wrapper for sqlite yet.

And secuencial work ok but feel "slow" when load several records.

uprise78

unread,

Apr 12, 2010, 8:05:39 PM4/12/10

to Three20

There are thread-safe sqlite wrappers/alternatives. The easiest one
is to just use NSOperationQueue with a maxConcurrentOperationCount of
0. I use FMDB with an NSOperationQueue and it works perfectly. I
have seen a couple other thread save sqlite wrappers out there as well
and if you compile sqlite yourself you can just enable the thread safe
flag.

mamcx

unread,

Apr 12, 2010, 11:06:59 PM4/12/10

to Three20

> There are thread-safe sqlite wrappers/alternatives.

Where?? the only I found is EGODatabase and is buggy...

> is to just use NSOperationQueue with a maxConcurrentOperationCount of
> 0.  I use FMDB with an NSOperationQueue and it works perfectly.  I
> have seen a couple other thread save sqlite wrappers out there as well
> and if you compile sqlite yourself you can just enable the thread safe
> flag.

I get the sqlite code & FMDB. I know how enable the sqlite threading
but my attemps failed. Can you point me to some example of this?

Natasha Tenn

unread,

Apr 13, 2010, 2:56:32 PM4/13/10

to thr...@googlegroups.com

I'm still a bit confused on how to implement this based on uprise78 suggestions.  I got things working by implementing it directly within a TTListDataSource datasource and placing the calls with the tableViewDidLoadModel.  I'd like to see if I can use the TTURLRequest method so I can use the TTURLRequestModel for caching, etc.  Has anyone have examples?


Drew McAuliffe

unread,

Apr 13, 2010, 4:02:30 PM4/13/10

to thr...@googlegroups.com

I don't think you're going to be able to use the tturlrequest model, since it caches URL requests, unsurprisingly. You need a a custom ttmodel, not a tturlrequest model. 

Sent from my iPad

Natasha Tenn

unread,

Apr 13, 2010, 4:52:22 PM4/13/10

to thr...@googlegroups.com

That's what I thought.  After spending some more time on this, I think I almost have it working.  In my hack, I faked the URL requests and populated a custom TTModel so I will bypass using the TTURLRequest all together.

mamcx

unread,

Apr 13, 2010, 10:48:09 PM4/13/10

to Three20

Want to share how?

mamcx

unread,

Apr 14, 2010, 6:05:21 PM4/14/10

to Three20

Fabien Penso

unread,

Apr 18, 2010, 9:06:09 PM4/18/10

to thr...@googlegroups.com

Just curious, why didn't you go using CoreData instead of plain sqlite/fmdb ?

bendell

unread,

Apr 22, 2010, 12:49:15 PM4/22/10

to Three20

Hi. Would you mind sharing your code to queue your FMDB connections?
I'm facing the same issue and could do with a couple of pointers.

uprise78

unread,

Apr 22, 2010, 5:33:55 PM4/22/10

to Three20

To queue connections just simply use a NSOperationQueue and set the
max operations to 1.

mamcx

unread,

Apr 23, 2010, 6:06:10 PM4/23/10

to Three20

On 18 abr, 14:06, Fabien Penso <fabienpe...@gmail.com> wrote:
> Just curious, why didn't you go using CoreData instead of plain sqlite/fmdb ?

Because I need full control on the schema of the db and do several
operations that are far more easy with plain sql than CoreData. My app
(http://www.bestsellerapp.com) is for make invoices, orders and that
stuff and sync with several ERPs (currently 12+) so I need a clear
schema and easy mapping. The sqlite db is read in a host/server
computer by python & delphi code, so CoreData is out the question
because is proprietary to obj-c.

And to be honest, I don't think coredata is easier than raw sql ;)

mamcx

unread,

Apr 23, 2010, 6:10:40 PM4/23/10

to Three20

On 22 abr, 05:49, bendell <b...@rawjam.co.uk> wrote:
> Hi. Would you mind sharing your code to queue your FMDB connections?
> I'm facing the same issue and could do with a couple of pointers.

I understand that feeling ;)

Relevant code:

From chibiORM:

@implementation DbBackground

@synthesize sql, db, delegate;

-(id)initWithSQL:(NSString *)theSql name:(NSString *)name
{
if (self = [super init]) {
self.sql = theSql;
self.db = [[DbPool sharedInstance] getConn:name];
}

return self;
}

- (void)dealloc
{
[sql release];
[db release];

[super dealloc];
}

-(void)main {
NSArray *results = [self.db loadAsDictArray:sql];
//sleep(2); For testing...
if( [delegate respondsToSelector:@selector(handleSqlResult:)])
{
[delegate performSelectorOnMainThread:@selector(handleSqlResult:)
withObject:results waitUntilDone:YES];
}
}

@end

In the Model:

- (id)init
{
if ((self = [super init])) {
queue = [[NSOperationQueue alloc] init];
//queue.maxConcurrentOperationCount = 1; //For sqlite. But I don't
set this with my queue code, become unnecesary
}

return self;
}

- (void)loadData:(NSString *)sql {
if (!self.availableRecords) {
return;
}
_isSearching=YES;
[self cleanData];
_lastSql = [sql copy];

DbBackground *op = [[[DbBackground alloc] initWithSQL:sql
name:@"background"] autorelease];
op.delegate = self;

[queue addOperation:op];
}

// Where get the result
- (void)handleSqlResult:(NSArray *)results
{
self.totalRecords = [results count];

self.data = (NSMutableArray *)results;
_isSearching=NO;
[self didChange];

Reply all

Reply to author

Forward