How improve scrolling with +5000 records sqlite table

Discussion in 'iPhone/iPad Programming' started by mamcx, Apr 7, 2009.

  1. macrumors regular

    Joined:
    Mar 13, 2008
    #1
    Hi,

    I have a sqlite database similar to the Apple contacts app.

    I do all the tricks I know to get a smoth operation, but I have a problem.

    I load the data in 50 recods blocks. Then, when the user scroll, request next 50 until finish the list.

    However, load that 50 records cause a notable "pause" in loading & scrolling. Everything else work fine.

    I cache the data, have opaque cells, draw it by code, etc...

    This is the code:

    I wonder what to do. Maybe using a async loading? or threads?

    No using objects and put the data in a array?
     
  2. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #2
    It looks like you know what you're doing but as you're seeing the biggest slow down on loading the next 50 maybe you should reduce the size of the blocks down to maybe 20? I don't know how complex your cells are....are you precompositing them into a single view (this probably isn't your problem though).
     
  3. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #3
    If you're not already doing your database/list load in a background thread then you definitely should be. Take a look at performSelectorInBackground:withObject:.

    In order to do updates to the UI you'll have to call the main thread from the background thread (the UI ignores all threads but the main), for which you'll need performSelectorOnMainThread:withObject:waitUntilDone:.

    Backgrounding your database accesses will prevent the main (UI) thread being blocked, so should make your app much more responsive (i.e. it should remove the pause you're seeing now).
     
  4. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    You should spend some time optimizing your sql and profiling and optimizing the code that runs while scrolling. You should definitely try different chunk sizes down to five or less to find the best size for good scrolling speed. Smaller may be better.

    Do those things before you look at threading this code. Threads will make this whole process much more complicated.
     
  5. thread starter macrumors regular

    Joined:
    Mar 13, 2008
    #5

    I put it to 5 (the # of visible cells) and the lagh is there anyway (obviously is less than with 50).

    I don't see where focus the optimization. The cells are drawing with drawRect.

    I will investigate the use of threads then...
     
  6. thread starter macrumors regular

    Joined:
    Mar 13, 2008
    #6
    I triying with NSOperation:

    Code:
    - (void) run:(NSString *)sql {
    	NSArray *data = [NSArray arrayWithObjects:
    					 sql,
    					 [self returnItemClass],
    					 nil];
    
    	NSInvocationOperation *operation = 
    	[[NSInvocationOperation alloc] initWithTarget:self 
    										 selector:@selector(loadRecords:) 
    										   object:data]; 
    	[self.queue addOperation:operation];
    	[operation release];
    }
    
    - (void) loadRecords:(NSArray *)data {
    	NSLog(@"Runing sql");
    	NSString * sql = [data objectAtIndex:0];
    	Class cls = [data objectAtIndex:1];
    
    	Db *db= [Db currentDb];
    	
    	NSArray *list = [db loadAndFill:sql theClass:cls];
    	[UIAppDelegate performSelectorOnMainThread:@selector(recordsLoaded:)
                                               withObject:list
                                            waitUntilDone:YES];
    }
    
    - (void) recordsLoaded:(NSArray *)data {
    	NSLog(@"Loading sql");
    	for (DbObject *o in data) {
    		[self.objectCache setObject:o forKey:[NSNumber numberWithInt:o.Id]];
    	}	
    }
    
    But I'm getting "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[JhonSellAppDelegate recordsLoaded:]: unrecognized selector sent to instance"

    Any idea in how code this rigth?
     

Share This Page