0

It is a very simple example. I cannot explain why these lines of code cause a crash. I only want to create multiple threads in a for loop. In the AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    for (int i = 0; i < 5; i++) {

        [self performSelectorInBackground:@selector(workInBackground)
                               withObject:nil];
    }
    return YES;
}

-(void)workInBackground{

}

The app crashes only sometimes (lldb crash, iPhone Simulator 5.0 - 6.1, Xcode Version 4.6.2). I use ARC.

Can anyone explain this behavior?

Update

I "solved" the problem. When I perform a cleanup before each test, the crash no longer occurs.

Natascha
  • 83
  • 1
  • 4

2 Answers2

0

I don't know what the problem is, but you should check this out: What are the differences between didFinishLaunchingWithOption and viewDidLoad

Also you could use NSOperationQueue if you have multiple task that you want to run in background.

Community
  • 1
  • 1
  • The problem is not the use of didFinishLaunchingWithOption or viewDidLoad. The behavior in this case is the same. – Natascha Jun 08 '13 at 12:07
  • I "solved" the problem. When I perform a cleanup before each test, the crash no longer occurs. – Natascha Jun 08 '13 at 13:20
0

Ok, so if you read the apple documetation they say: "This method is called after state restoration has occurred but before your app’s window and other UI have been presented. At some point after this method returns, the system calls another of your app delegate’s methods to move the app to the active (foreground) state or the background state." this mean that actually the app in this point in not yet in the foreground state, here you should store somewhere the intention to start your threads, and after that the sistem call the method - (void)applicationDidBecomeActive:(UIApplication *)application, at this point you are absolutelly sure that your app is in a foreground state and active, now if you have recorded the intention to start your threads you can do it, and don't forget to reset the intention variable status

Manu
  • 788
  • 5
  • 10
  • It is not the problem. The same thing happens when I do it in the viewDidLoad-method of a viewController. – Natascha Jun 08 '13 at 13:15