Process :
- App is on the home view controller and is requesting data on API to set an NSObject property. The request is processing on a private method.
- User change the view controller to a second view controller (the request is still processing asynchronously)
- The second view controller is loaded
- The request is ending and app return EXC_BAD_ACCESS when it setting the object property
It seems the object has not the correct memory access.
I would like than the user can switch view controller, even if there is a request pending, and the application doesn't crash.
I don't want to block the user on the view controller during loading.
User.h
@interface User : NSObject
[...]
@property (nonatomic) NSString *notification;
[...]
-(void) methodName;
@end
User.m
-(void) methodName{
//there is code around but useless for this problem
[...]
NSError *error;
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
self.notification = [[dict objectForKey:@"infos"] objectForKey:@"notification"]; //Here is the EXC_BAD_ACCESS
[...]
}
MyController.m
@interface MyController ()
@end
User *user;
@implementation HomeCVViewController
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
user = [User new];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[user someMethod];
dispatch_async(dispatch_get_main_queue(), ^{
[...]//some code
});
});
}
@end
EDIT :
I just put @property (nonatomic) User *user;
in MyController.h and remove User *user;
in MyController.m . The user is not deallocated and there is no crash.
[dict objectForKey:@"infos"]
is notNSNull
- Crash can be here.-(void)dealloc
to your object and put a break point there to verify that the object is not being released before the assignment.