Note that there are some explanatory texts on larger screens.

plurals
  1. POMultithreading problem: code crashes at specific point
    primarykey
    data
    text
    <p>I just introduced multithreading into my app, and now it crashes at a specific point in the program, whilst if I don't do multithreading it does work.</p> <p>FlipsideViewController.h</p> <pre><code>// // FlipsideViewController.h // iRSG // // Created by Thomas van Arkel on 12-02-11. // Copyright 2011 Arques Software. All rights reserved. // #import &lt;UIKit/UIKit.h&gt; @protocol FlipsideViewControllerDelegate; @interface FlipsideViewController : UIViewController { id &lt;FlipsideViewControllerDelegate&gt; delegate; NSArray *timeTableArray; IBOutlet UIActivityIndicatorView *activityIndicator; } - (IBAction)getTimeTable:(UIButton *)sender; - (IBAction)removeTimeTable:(UIButton *)sender; @property (nonatomic, assign) id &lt;FlipsideViewControllerDelegate&gt; delegate; - (IBAction)done:(id)sender; @end @protocol FlipsideViewControllerDelegate - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller; @end </code></pre> <p>FlipsideViewController.m</p> <pre><code>// // FlipsideViewController.m // iRSG // // Created by Thomas van Arkel on 12-02-11. // Copyright 2011 Arques Software. All rights reserved. // #import "FlipsideViewController.h" #import "TimeTableCreator.h" #import "Lesson.h" #import "iRSGAppDelegate.h" @interface FlipsideViewController() @property (nonatomic, retain) IBOutlet UIActivityIndicatorView *spinner; @end @implementation FlipsideViewController @synthesize delegate; @synthesize spinner; - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor groupTableViewBackgroundColor]; } - (IBAction)done:(id)sender { [self.delegate flipsideViewControllerDidFinish:self]; } - (IBAction)getTimeTable:(UIButton *)sender { activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0); activityIndicator.center = self.view.center; [self.view addSubview: activityIndicator]; [activityIndicator startAnimating]; dispatch_queue_t downloadQueue = dispatch_queue_create("TimeTable downloader", NULL); timeTableArray = [[NSArray alloc] init]; dispatch_async(downloadQueue, ^{ NSString* settingValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"partOfSchool"]; if ([settingValue isEqualToString:@"upper"]) { NSString *studentID = [[NSUserDefaults standardUserDefaults] stringForKey:@"studentID"]; NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://roosters.rsgslingerboslevant.nl/lichtkrant/slingerbos/leerlingen/ll_roost_%@.htm", studentID]]; timeTableArray = [TimeTableCreator createTimeTableWithURL:url]; } else if ([settingValue isEqualToString:@"lower"]) { NSString *class = [[NSUserDefaults standardUserDefaults] stringForKey:@"class"]; if (class.length == 2) { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://roosters.rsgslingerboslevant.nl/lichtkrant/Slingerbos/Klassen/K_%@.htm", class]]; timeTableArray = [TimeTableCreator createTimeTableForLowerClassWithURL:url]; } else if (class.length == 3) { NSString *beginningString = [class substringToIndex:1]; NSString *endString = [class substringFromIndex:2]; endString = [endString lowercaseString]; class = [beginningString stringByAppendingString:endString]; NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://roosters.rsgslingerboslevant.nl/lichtkrant/Slingerbos/Klassen/K_%@.htm", class]]; timeTableArray = [TimeTableCreator createTimeTableForLowerClassWithURL:url]; } } dispatch_async(dispatch_get_main_queue(), ^{ NSManagedObjectContext *managedObjectContext = nil; if (managedObjectContext == nil) { iRSGAppDelegate *appDelegate = (iRSGAppDelegate *)[[UIApplication sharedApplication] delegate]; managedObjectContext = [appDelegate managedObjectContext]; } NSFetchRequest * allLessons = [[NSFetchRequest alloc] init]; [allLessons setEntity:[NSEntityDescription entityForName:@"Lesson" inManagedObjectContext:managedObjectContext]]; [allLessons setIncludesPropertyValues:NO]; //only fetch the managedObjectID NSError * error = nil; NSArray * lessons = [managedObjectContext executeFetchRequest:allLessons error:&amp;error]; NSLog(@"Amount of lessons is %@", [NSNumber numberWithInt:[lessons count]]); [allLessons release]; //error handling goes here for (NSManagedObject * lesson in lessons) { [managedObjectContext deleteObject:lesson]; } iRSGAppDelegate *appDelegate = (iRSGAppDelegate *)[[UIApplication sharedApplication] delegate]; [appDelegate saveContext]; [appDelegate saveContext]; for (NSArray *lessonArray in timeTableArray) { [Lesson lessonWithLessonArray:lessonArray inManagedObjectContext:managedObjectContext]; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Het rooster is succesvol geïmporteerd"delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; [alert release]; [activityIndicator stopAnimating]; }); }); dispatch_release(downloadQueue); } - (IBAction)removeTimeTable:(UIButton *)sender { NSManagedObjectContext *managedObjectContext = nil; if (managedObjectContext == nil) { iRSGAppDelegate *appDelegate = (iRSGAppDelegate *)[[UIApplication sharedApplication] delegate]; managedObjectContext = [appDelegate managedObjectContext]; } NSFetchRequest * allLessons = [[NSFetchRequest alloc] init]; [allLessons setEntity:[NSEntityDescription entityForName:@"Lesson" inManagedObjectContext:managedObjectContext]]; [allLessons setIncludesPropertyValues:NO]; //only fetch the managedObjectID NSError * error = nil; NSArray * lessons = [managedObjectContext executeFetchRequest:allLessons error:&amp;error]; NSLog(@"Amount of lessons is %@", [NSNumber numberWithInt:[lessons count]]); [allLessons release]; //error handling goes here for (NSManagedObject * lesson in lessons) { [managedObjectContext deleteObject:lesson]; } iRSGAppDelegate *appDelegate = (iRSGAppDelegate *)[[UIApplication sharedApplication] delegate]; [appDelegate saveContext]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } /* // Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } */ - (void)dealloc { [super dealloc]; } @end </code></pre> <p>The program always crashes here</p> <pre><code> for (NSArray *lessonArray in timeTableArray) { [Lesson lessonWithLessonArray:lessonArray inManagedObjectContext:managedObjectContext]; } </code></pre> <p>Thanks in advance</p> <p><strong>EDIT</strong> Source code <a href="http://cl.ly/3k322a472A0Q0A3F0S1G" rel="nofollow">http://cl.ly/3k322a472A0Q0A3F0S1G</a></p> <p>To test it, in settings app enter '2007042' in for 'leerlingnummer' and for 'onder/bovenbouw' choose 'bovenbouw' (sorry for the fact that it is in Dutch, I'm writing the code for a Dutch school</p> <p>I get a EXC_BAD_ACCESS error, nothing is on the console. <strong>SOLVED</strong> I made a new timeTableArray property and when setting the array, i used self.timeTableArray. This was why it said that it was deallocated (thanks to NSZombie)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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