OK, this is really desperate and painful, and I am now stuck and hungry in office
Basically, I only need to run some operations on IPhone after the execution of main UI thread. And I used NSOperation and NSOperationQueue for this. I checked the SDK, and here is what I wrote for testing this behaviour:
I got a simple class inherit from NSOperations
TestLoader.h
TestLoader.m
and then in my appDelegate.m, I put this into applicationDidFinishLaunching
and this is the output:
so, basically:
1. the main thread is blocked until all 2 threads are done
2. the second thread is not added until first thread is done
I googled around for 2 hours, and found some sample code in Cocoa, but none is for iPhone, anyone any hint please?!!!
thanks a lot
Basically, I only need to run some operations on IPhone after the execution of main UI thread. And I used NSOperation and NSOperationQueue for this. I checked the SDK, and here is what I wrote for testing this behaviour:
I got a simple class inherit from NSOperations
TestLoader.h
Code:
#import <UIKit/UIKit.h>
@interface TestLoader : NSOperation {
bool running,started;
int threadID;
}
-(id)initWithThreadID:(int)threadID_;
@end
TestLoader.m
Code:
#import "TestLoader.h"
@implementation TestLoader
-(id)initWithThreadID:(int)threadID_
{
self = [super init];
running = FALSE;
started = FALSE;
threadID = threadID_;
return self;
}
- (BOOL)isExecuting
{
return running;
}
- (BOOL)isFinished
{
return started && !running;
}
- (void)start
{
running = true;
started = TRUE;
sleep(3);
for(int i=0; i<10; i++)
NSLog(@"@17,inside thread %d ", threadID);
running = false;
}
- (BOOL)isConcurrent
{
return YES;
}
and then in my appDelegate.m, I put this into applicationDidFinishLaunching
Code:
//queue is of NSOperationQueue object and is instantiated in init:
[queue setMaxConcurrentOperationCount:2];
TestLoader *loader = [[TestLoader alloc]initWithThreadID:1];
[queue addOperation:loader];
NSLog(@"@53,add new thread");
[queue addOperation:[[TestLoader alloc]initWithThreadID:2]];
[loader release];
NSLog(@"@37, about to quite main thread");
and this is the output:
Code:
2008-09-29 20:03:04.273 Rosetta[13090:20b] @17,inside thread 1
2008-09-29 20:03:04.275 Rosetta[13090:20b] @17,inside thread 1
2008-09-29 20:03:04.278 Rosetta[13090:20b] @17,inside thread 1
2008-09-29 20:03:04.278 Rosetta[13090:20b] @17,inside thread 1
2008-09-29 20:03:04.279 Rosetta[13090:20b] @17,inside thread 1
2008-09-29 20:03:04.279 Rosetta[13090:20b] @17,inside thread 1
2008-09-29 20:03:04.280 Rosetta[13090:20b] @17,inside thread 1
2008-09-29 20:03:04.280 Rosetta[13090:20b] @17,inside thread 1
2008-09-29 20:03:04.281 Rosetta[13090:20b] @17,inside thread 1
2008-09-29 20:03:04.281 Rosetta[13090:20b] @17,inside thread 1
2008-09-29 20:03:04.282 Rosetta[13090:20b] @53,add new thread
2008-09-29 20:03:07.282 Rosetta[13090:20b] @17,inside thread 2
2008-09-29 20:03:07.284 Rosetta[13090:20b] @17,inside thread 2
2008-09-29 20:03:07.284 Rosetta[13090:20b] @17,inside thread 2
2008-09-29 20:03:07.285 Rosetta[13090:20b] @17,inside thread 2
2008-09-29 20:03:07.286 Rosetta[13090:20b] @17,inside thread 2
2008-09-29 20:03:07.287 Rosetta[13090:20b] @17,inside thread 2
2008-09-29 20:03:07.287 Rosetta[13090:20b] @17,inside thread 2
2008-09-29 20:03:07.288 Rosetta[13090:20b] @17,inside thread 2
2008-09-29 20:03:07.288 Rosetta[13090:20b] @17,inside thread 2
2008-09-29 20:03:07.289 Rosetta[13090:20b] @17,inside thread 2
2008-09-29 20:03:07.289 Rosetta[13090:20b] @37, about to quite main thread
so, basically:
1. the main thread is blocked until all 2 threads are done
2. the second thread is not added until first thread is done
I googled around for 2 hours, and found some sample code in Cocoa, but none is for iPhone, anyone any hint please?!!!
thanks a lot