Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mikezang

macrumors 6502a
Original poster
May 22, 2010
930
38
Tokyo, Japan
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?
Code:
-(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;
	}
}
 
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.
 
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:)
 

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/l...asses/NSThread_Class/Reference/Reference.html
http://developer.apple.com/iphone/l...troduction.html#//apple_ref/doc/uid/10000057i
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.