Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS threading, second thread can't see variables?
    text
    copied!<p>I'm going to try and explain this without showing any code because I don't think I need to...</p> <p>I have an object i've created that contains data and several methods for accessing data from a web-server, in which case it then populates said data for later retrieval.</p> <p>In the main thread, when the view initializes, I initialize this object with the URL it needs to access the data. This is all it does when it's initialized... Then, once the view loads, I call one of the object's methods which then grabs the data from the web-server. This can take several seconds or longer deepening on what data is being retrieved. After that, I can call other methods to get the different kinds of data that I need.</p> <p>My problem is i can't seem to find the best way to go about putting this into another thread. Doing a simple performSelectorInBackground doesn't seem to work because the object I initialized when the view initialized can't be accessed. The second thread can't see it for what ever reason. I can, in theory, initialize the object and run the methods I need to run inside the second thread. Then pass the object back to the main thread... I'm looking at possibly doing a performSelectorOnMainThread and passing the object back that way. Would that work?</p> <p>Ideally, it would be nice to have something that will let me:</p> <ol> <li>Run some code in another thread...</li> <li>Wait for said code to finish...</li> <li>Continue on with the rest of the program.</li> </ol> <p>Its doesn't seem like it's that easy unfortunately. Any help that points me in the right direction here would be greatly appreciated. Or at least confirm that my theory about passing the object back would suffice.</p> <p>EDIT: How can I read from and modify an instance variable i've defined in my header file for my view controller? When I put a break point in the method that is run in the second thread, it's like the variable hasn't been initialized.</p> <p>EDITx2:</p> <pre><code>- (id)initWithText:(NSString *)strURL{ if (self) { // Custom initialization osSearch = [[orgSearch alloc] initWithurl:strURL]; } return self; } - (void)viewDidLoad{ [self loadSearches]; [super viewDidLoad]; } - (void)loadSearches{ [self performSelectorInBackground:@selector(workerThread) withObject:nil]; } -(void) workerThread{ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; [osSearch go]; [self performSelectorOnMainThread:@selector(returnToMain) withObject:nil waitUntilDone:FALSE]; [pool release]; } -(void)returnToMain{ UITableView* tblView = tableView; tblView.frame = CGRectMake(0,28,320,387); [tblView release]; } </code></pre> <p>osSearch is the object i'm trying to access but it seems like all my ivars are being dealloced. if I put a break on the the performSelectorInBackground, I can see that osSearch was initialized correctly. Once i enter workerThread, it's like it never was. I can comment out the call to "go" and just return to the main thread and it will still look like it's never been initialized.</p> <p>Just as a test, I created a new project, using the same threading method and object, I was able to create the new thread and access the view's ivars and the objects ivars just fine. I don't understand whats happening here. My only guess is the garbage collecter doesn't like me, but thats just a guess. Again, any help would be great.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload