PDA

View Full Version : How can I speedup list for table view?




mikezang
Aug 18, 2010, 07:51 AM
I use some code as below to prepare list for my table view, it is very slow, is there any faster way to make list?

-(BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSBundle *bundle = [NSBundle mainBundle];
NSString *filePath = [bundle pathForResource:@"index" ofType:@"txt"];
NSString *contents = [NSString stringWithContentsOfFile:filePath encoding:NSShiftJISStringEncoding error:nil];
NSArray *list = [contents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
int i = 0;

if (list) {
NSMutableArray *stock = [[NSMutableArray alloc] init];

for (NSString *item in list) {
if ([item length] != 0 && i != 0) {
NSRange obs = [item rangeOfString:@"OBS"];

if (obs.location == NSNotFound) {
obs = [item rangeOfString:@"A100"];

if (obs.location == NSNotFound) {
[stock addObject:item];
}
}
}

i++;
}

stockSplit = stock;
}
}



robbieduncan
Aug 18, 2010, 08:15 AM
Since you are loading this file from within your application bundle you are 100% in control of the format. Make it a plist in a format that is basically what you want instead of parsing a massive string. The parsing is what is slow.

mikezang
Aug 18, 2010, 08:26 AM
At the moment, I just test my logic so I put file in SandBox, in fact, this file is downloaded from Internet, so that I can't modify its format:)

chown33
Aug 18, 2010, 11:47 AM
Post a sample of the data to be parsed.

Have you looked at NSScanner?

Anne R. Kist
Aug 18, 2010, 12:00 PM
Processing like this should not be done in the main run loop but moved to a background thread instead.

mikezang
Aug 18, 2010, 06:26 PM
Post a sample of the data to be parsed.

Have you looked at NSScanner?
No, I haven't.

mikezang
Aug 18, 2010, 06:27 PM
Processing like this should not be done in the main run loop but moved to a background thread instead.
I also think so, but I am not sure how to do it, can you tell me where I can find a useful sample for background thread?

dejo
Aug 18, 2010, 07:11 PM
I also think so, but I am not sure how to do it, can you tell me where I can find a useful sample for background thread?
Advice for lengthy App startup processes (http://www.iphonedevsdk.com/forum/iphone-sdk-development/46085-advice-lengthy-app-startup-processes.html)

chown33
Aug 18, 2010, 07:30 PM
Advice for lengthy App startup processes (http://www.iphonedevsdk.com/forum/iphone-sdk-development/46085-advice-lengthy-app-startup-processes.html)

Nice link.

To answer the unanswered question there:
As an aside, as the main thread is running in the main.c/autorelease pool, is there any issue with creating a separate nested autorelease pool in the worker thread method?

Every thread has its own separate autorelease pool(s). The new thread's pool is not nested with any other thread's pool or pools. This is discussed in the documentation, which everyone should read who plans on copying and pasting code using NSThread:
http://developer.apple.com/iphone/library/documentation/cocoa/reference/foundation/Classes/NSThread_Class/Reference/Reference.html
http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/Multithreading/Introduction/Introduction.html#//apple_ref/doc/uid/10000057i

mikezang
Aug 18, 2010, 09:22 PM
Advice for lengthy App startup processes (http://www.iphonedevsdk.com/forum/iphone-sdk-development/46085-advice-lengthy-app-startup-processes.html)
Thanks for your link, it is very useful.

ianray
Aug 19, 2010, 05:11 PM
See also: http://news.atebits.com/post/197580827/fast-scrolling-in-tweetie-with-uitableview :)