Can I use a background thread to parse data?

后端 未结 4 1892
闹比i
闹比i 2021-01-07 10:35

I\'m using chcsvparser to parse data from a csv file on my apps launch. It\'s taking way too long to startup on main thread so I was thinking of doing this on the backgroun

4条回答
  •  误落风尘
    2021-01-07 11:14

    The NSObject class has several different instance methods that allow you to invoke methods on the main UI thread. The performSelectorOnMainThread:withObject:waitUntilDone: method, for instance, allows you to invoke a method of the receiver on the main thread with the object of your choice.

    Here's some code to get you started:

    -(void) dataDoneLoading:(id) obj {
        NSMutableArray *array = (NSMutableArray *) obj;
        // update your UI
        NSLog(@"done");
    }
    
    -(void) myThread:(id) obj {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSMutableArray *array = [[[NSMutableArray alloc]init ]autorelease];
    
        // build up your array
    
        [self performSelectorOnMainThread:@selector(dataDoneLoading:) withObject:array waitUntilDone:NO];
    
        [pool release];    
    }
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
        [NSThread detachNewThreadSelector:@selector(myThread:) toTarget:self withObject:nil];    
    }
    

提交回复
热议问题