How can I speedup list for table view?

Discussion in 'iPhone/iPad Programming' started by mikezang, Aug 18, 2010.

  1. macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    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;
    	}
    }
    
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    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:)
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    Post a sample of the data to be parsed.

    Have you looked at NSScanner?
     
  5. macrumors newbie

    Joined:
    Aug 11, 2010
    Location:
    ...in the UK
    #5
    Processing like this should not be done in the main run loop but moved to a background thread instead.
     
  6. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #6
    No, I haven't.
     
  7. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #7
    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?
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
  9. macrumors 603

    Joined:
    Aug 9, 2009
    #9
    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
     
  10. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #10
  11. macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @

Share This Page