I'm experiencing a strange issue related to "pass by reference" pattern, in my code each time I call a method like:
[foo doSomethingWithError:&error];
it freezes my app... no exception is raised! If I replace &error with nil, the code execution can then proceed... but why? It works has expected in iOS 4+
I also tried to use __autoreleasing for error (as documented by apple here: https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/_index.html), but I'm unable to fix the issue :(
ps: I'm using ARC
EDIT:
this is an example of method that causes a freeze:
- (BOOL)loadData {
NSError *error = nil;
if ([self.fetchController pe开发者_StackOverflow中文版rformFetch:&error]) {
[self.tableView reloadData];
GTMLoggerDebug(@"Data loaded!");
return YES;
} else {
GTMLoggerDebug(@"Error while fetching data: %@", [error description]);
return NO;
}
}
the method is called in viewDidLoad, but it doesn't work neither in viewDidAppear.
fetchController is declared as:
@property (readonly, strong) NSFetchedResultsController *fetchController;
Some test I used in loadData (all passed):
NSAssert(self.fetchController != nil, @"NO FETCH CONTROLLER");
NSAssert([self.fetchController respondsToSelector:@selector(performFetch:)], @"CAN'T FETCH?!");
NSAssert(self.fetchController.managedObjectContext != nil, @"invalid context");
NSAssert(self.fetchController.fetchRequest != nil, @"invalid fetchRequest");
NSAssert(self.fetchController.delegate != nil, @"invalid delegate");
NSAssert([NSThread isMainThread], @"NOT MAIN THREAD!");
then in viewDidLoad:
[self performSelectorOnMainThread:@selector(loadData) withObject:nil waitUntilDone:YES/NO];
...but nothing... it does not work... or better, it works very very rarely :(
SOLVED!
After hours of tests and reasoning about it, I realized that the real problem was related to core data. I was using an NSManagedObjectContext shared between threads, and since context are NOT thread safe, I was experiencing these strange issues!
精彩评论