Reputation: 35
I have created a custom button in ViewDidLoad
that shows a new view (via downALevel):
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(downALevel:)];
self.navigationItem.rightBarButtonItem = addButton;
The view loads fine and the button works. When I return to the view, the buttons appears to be disabled and won't work. I'm using Xcode 6; this wasn't an issue in previous versions.
Any ideas?
(IBAction)downALevel:(id)sender{
[self showWaiting];
[self performSelectorInBackground:(@selector(callGrouper4)) withObject:nil];
}
-(void)showWaiting{
waitAlert = [[UIAlertView alloc] initWithTitle:@"Please Wait...." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[waitAlert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(waitAlert.bounds.size.width / 2, waitAlert.bounds.size.height - 50);
[indicator startAnimating];
[waitAlert addSubview:indicator];
}
-(void)callGrouper4{ // down a level
@autoreleasepool {
float progressPercent;
NSString *progMsg;
NSString *curProgress;
NSNumberFormatter *formatter2 = [[NSNumberFormatter alloc] init];
[formatter2 setNumberStyle: NSNumberFormatterDecimalStyle];
[formatter2 setMinimumFractionDigits:0];
[formatter2 setMaximumFractionDigits:0];
NSManagedObjectContext *newMoc = [[NSManagedObjectContext alloc] init];
NSPersistentStoreCoordinator *coordinator2 = [[self managedObjectContext] persistentStoreCoordinator];
[newMoc setPersistentStoreCoordinator:coordinator2];
[newMoc setUndoManager:nil];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"TrialBalance" inManagedObjectContext:newMoc]];
......lots of data manipulation......
//finish
[waitAlert dismissWithClickedButtonIndex:0 animated:TRUE];
reportBalances2 = nil;
Level2ActivityViewController *newController = [[Level2ActivityViewController alloc] initWithNibName:@"Level2ActivityViewController" bundle:[NSBundle mainBundle]];
[newController setManagedObjectContext:newMoc];
[newController setHeaderTitle:headerTitle];
[newController setLevelToQuery:@"Obj2"];
[newController setDdSumOrDetail:ddSumOrDetail];
[newController setOrgNameVar:orgNameVar];
[newController setPeriodNum:periodNum];
[newController setReportMode:reportMode];
[newController setSectionSort:@"obj1"];
[[self navigationController] pushViewController:newController animated:YES];
}
UPDATE 1 Let's call the first view A and the new view B. I just discovered that if B has the same code to create a custom button, then the button on A is disabled. If the code is absent from B then all is well. I'm very confused??!!
Upvotes: 0
Views: 160
Reputation: 2870
It seems like you are violating a couple of thread responsibilities here!
First, you are calling performSelectorInBackground:
but inside the callGrouper4
method, you are not switching back to the main thread before pushing a view controller. Keep in mind that UIKit is not thread safe. This could case strange behaviour like the one you are seeing!
Also you are creating an NSManagedObjectContext
without using the initWithConcurrencyType:
api. This means your managed object context is using the NSConfinementConcurrencyType
concurrency type. This means you are not allowed to pass this context to another thread, but you are assigning it to the new view controller, which will, most likely, make calls to it on the main thread, which is not the thread it was created in.
When creating your NSManagedObjectContext
you are also setting the coordinator like this: [[self managedObjectContext] persistentStoreCoordinator]
. I would assume the managed object context you are querying here has been created in the same way, probably on another thread. In this case it is not safe to make this call either!
While issue two and three might result in unexpected behaviour later on, issue one could easily be the problem why your UI behaves strangely.
Upvotes: 1