0

Imagine I have this method I want to call in the background.

- (void)loadView
{
    [super loadView];
    //Make a button which will fire the "buttonClicked" method
    //Make my own uitableview.
    //Fire method: "theBackGroundMethod" in the background.
}

I'm sorry about the vagueness but I'll try to be as clear as I can. The following is what I want to do. I have a view, I but this button on it and an empty UITableView. Then I want theBackGroundMethod to run the the background, while this is running I want to be able to click the button and fire the buttonClicked method which will shoot data into the UITableView.

The following I have tried with negative results:

[self performSelectorInBackground:@selector(theBackGroundMethod) withObject:nil];

This starts theBackGroundMethod but it doesn't load the data which I ask from the server.

[self performSelectorOnMainThread:@selector(theBackGroundMethod) withObject:nil waitUntilDone:NO];

This starts theBackGroundMethod but it doesn't allow me to click the button for some reason, well it allows be to click the button but every action I do while the method is firing the clicks are put in queue behind it, so only when the backgroundmethod is done the x times I pressed the button is handled.

Does anyone have an idea what might block the use of any of the performSelector-functions described above and how to resolve them. Or have an entire (maybe better) idea..?

Totumus Maximus
  • 7,543
  • 6
  • 45
  • 69
  • Can you post the code in your `theBackGroundMethod`? – kubi Oct 10 '11 at 15:55
  • Unfortunately I can't post the specifics. But I can describe the workings. theBackGroundMethod fires a method in some other class on his own and has some server interaction. First the method sends a request to the server to give data. Then it waits for a response and deals with the xml accordingly. When all the data is handled it should return to a delegate method made in the same class as the loadView (as described up here). – Totumus Maximus Oct 11 '11 at 07:21
  • Well, there's nothing wrong with the way you are using `performSelectorInBackground`. The problem is with your `theBackGroundMethod` code… but it's hard to debug if we can't see it. – kubi Oct 11 '11 at 13:32
  • I'm late. But, this link is useful to you or for everyone that need to call method on background. http://stackoverflow.com/questions/7055424/ios-start-background-thread – Ele Jan 17 '13 at 20:26

3 Answers3

0

If you're running async URL requests or anything like that, you'll need to run a runloop in the thread the requests are on. That would be why it works on the main thread, which already has a runloop.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • I'm connecting to a SOAP to get my data. But a NSRunloop might be very well what I need. Now to find out how to implement it.. ^^ – Totumus Maximus Oct 11 '11 at 08:16
  • Then I will probably need to change something in the "wait for response"-code. Which I didn't really want to touch there. Still I'm not sure what could stop a background thread like that and not handle the delegate method which he should do and has done in the MainThread – Totumus Maximus Oct 11 '11 at 13:44
  • @kubi: It sets up a runloop and loops it in kCFRunLoopModeDefault? I was not aware of that, and the threading docs indicate otherwise (http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html). I will have to double-check later, but I'm pretty sure it doesn't do that for you. – Chuck Oct 11 '11 at 15:50
  • @Chuck: You are right, I deleted my comment. – kubi Oct 11 '11 at 16:49
0

I have the same problem, I was using a UITable so in the methode running at the background, at the end of it I added this line

[self.tableView reloadData];

I don't know if your using a tableview, but this might get you someway to solve your problem, I should thank you for run in background ;)

shbli
  • 566
  • 1
  • 6
  • 14
  • Nope its not that easy I'm affraid ;) Its not getting the data at all. What you are suggesting is that the data isn't reloaded. – Totumus Maximus Oct 17 '11 at 12:45
-1

Try instead:

[NSThread detachNewThreadSelector:@selector(theBackGroundMethod:) toTarget:self withObject:nil];
onnoweb
  • 3,038
  • 22
  • 29
  • Gives me a SIGABRT when I run it. – Totumus Maximus Oct 10 '11 at 15:45
  • @selector(theBackGroundMethod:) if your method takes an input parameter. @selector(theBackGroundMethod) if not. – onnoweb Oct 10 '11 at 15:47
  • ah that seemed to fix the sigabrt, however it didn't solve my problem. It gave same negative results as performSelectorInBackground. theBackGroundMethod is fired but it doesn't return to the delegate method that it should return to normally. – Totumus Maximus Oct 11 '11 at 07:16
  • Might it be that the server call is put on halt since it is running in the background? The request got sent but no response is coming back. – Totumus Maximus Oct 11 '11 at 07:22